better sprites

c3
Alessandro Mauri 2 months ago
parent b317951c32
commit fbe631b4b4
  1. 16
      src/main.c3
  2. 7
      src/ugui_core.c3
  3. 16
      src/ugui_input.c3
  4. 32
      src/ugui_sprite.c3

@ -80,6 +80,8 @@ fn int main(String[] args)
{
ugui::Ctx ui;
ui.init()!!;
defer ui.free();
ui.load_font("font1", "resources/hack-nerd.ttf", 16)!!;
ui.sprite_atlas_create("icons", AtlasType.ATLAS_RGBA32, 512, 512)!!;
ui.import_sprite_file_qoi("tux", "resources/tux.qoi")!!;
@ -122,7 +124,12 @@ fn int main(String[] args)
io::printfn("%s", k);
} while (k != 0);
*/
for (int c; (c = rl::get_char_pressed()) != 0;) {
ugui::ModKeys mod;
mod.rctrl = rl::is_key_pressed(rl::KEY_RIGHT_CONTROL);
mod.lctrl = rl::is_key_pressed(rl::KEY_LEFT_CONTROL);
ui.input_mod_keys(mod);
for (int c; (c = rl::get_key_pressed()) != 0;) {
int[1] ts;
ts[0] = c;
ui.input_text_unicode(ts[..]);
@ -153,6 +160,8 @@ fn int main(String[] args)
/* Start UI Handling */
ui.frame_begin()!!;
if (ui.check_key_combo(ugui::KMOD_CTRL, "q")) break;
// main div, fill the whole window
ui.div_begin("main", ugui::Rect{.w=-100})!!;
{|
@ -179,12 +188,12 @@ fn int main(String[] args)
ui.slider_ver("slider_a", ugui::Rect{0,0,30,100}, &af)!!;
ui.layout_next_column()!!;
ui.text_unbounded("text1", "Ciao Mamma\nAbilità ⚡")!!;
ui.text_unbounded("text1", "Ciao Mamma\nAbilità ⚡\n'\udb80\udd2c'")!!;
ui.layout_next_column()!!;
ui.button_label("Continua!")!!;
|};
ui.draw_sprite("tux")!!;
ui.draw_sprite("sprite1", "tux")!!;
ui.div_end()!!;
ui.div_begin("second", ugui::DIV_FILL, scroll_x: true, scroll_y: true)!!;
@ -303,7 +312,6 @@ fn int main(String[] args)
rl::close_window();
ui.free();
return 0;
}

@ -17,6 +17,7 @@ enum ElemType {
ETYPE_BUTTON,
ETYPE_SLIDER,
ETYPE_TEXT,
ETYPE_SPRITE,
}
bitstruct ElemFlags : uint {
@ -47,6 +48,7 @@ struct Elem {
ElemButton button;
ElemSlider slider;
ElemText text;
ElemSprite sprite;
}
}
@ -324,7 +326,7 @@ fn ElemEvents Ctx.get_elem_events(&ctx, Elem *elem)
{
bool hover = ctx.is_hovered(elem);
bool focus = ctx.focus_id == elem.id || (hover && ctx.is_mouse_pressed(BTN_LEFT));
if (ctx.is_mouse_pressed(BTN_ANY) && !hover){
focus = false;
if (ctx.focus_id == elem.id) ctx.focus_id = 0;
@ -332,7 +334,7 @@ fn ElemEvents Ctx.get_elem_events(&ctx, Elem *elem)
if (hover) { ctx.hover_id = elem.id; }
if (focus) { ctx.focus_id = elem.id; }
ElemEvents ev = {
.mouse_hover = hover,
.mouse_press = hover && focus && ctx.is_mouse_pressed(BTN_ANY),
@ -341,4 +343,3 @@ fn ElemEvents Ctx.get_elem_events(&ctx, Elem *elem)
};
return ev;
}

@ -3,7 +3,7 @@ module ugui;
import grapheme;
import std::io;
import std::math;
import std::core::string;
bitstruct InputEvents : uint {
bool resize : 0; // window size was changed
@ -58,6 +58,20 @@ const MouseButtons BTN_5 = {.btn_5 = true};
const ModKeys KEY_ANY = (ModKeys)(ModKeys.inner.max);
fn bool Ctx.check_key_combo(&ctx, ModKeys mod, String key)
{
bool is_mod = (bool)(ctx.input.keyboard.down & mod);
bool is_keys = true;
String haystack = (String)ctx.input.keyboard.text[0..ctx.input.keyboard.text_len];
char[2] needle;
foreach (c: key) {
needle[0] = c;
is_keys = is_keys && haystack.contains((String)needle[..]);
}
io::printfn("%b %b", is_mod, is_keys);
return is_mod && is_keys;
}
// Window size was changed
fn void! Ctx.input_window_size(&ctx, short width, short height)
{

@ -21,6 +21,9 @@ struct SpriteAtlas {
bool should_update;
}
struct ElemSprite {
Id id;
}
// name: some examples are "icons" or "images"
fn void! SpriteAtlas.init(&this, String name, AtlasType type, ushort width, ushort height)
@ -118,13 +121,34 @@ fn void! Ctx.import_sprite_file_qoi(&ctx, String name, String path)
ctx.sprite_atlas.insert(name, pixels, (ushort)w, (ushort)h, (ushort)w)!;
}
// FIXME: test function, very different from every other function here
fn void! Ctx.draw_sprite(&ctx, String name)
fn void! Ctx.draw_sprite(&ctx, String label, String name, Point off = {0,0})
{
Id id = ctx.gen_id(label)!;
Elem *parent = ctx.get_parent()!;
Elem *elem = ctx.get_elem(id)!;
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
if (elem.flags.is_new) {
elem.type = ETYPE_SPRITE;
} else if (elem.type != ETYPE_SPRITE) {
return UgError.WRONG_ELEMENT_TYPE?;
}
Sprite* sprite = ctx.sprite_atlas.get(name)!;
Rect bounds = { 100, 100, sprite.w, sprite.h };
Rect uv = { sprite.u, sprite.v, sprite.w, sprite.h };
Rect bounds = { 0, 0, sprite.w, sprite.h };
elem.bounds = ctx.position_element(parent, bounds.off(off), true);
elem.sprite.id = ctx.get_sprite_atlas_id(name);
// if the bounds are null the element is outside the div view,
// no interaction should occur so just return
if (elem.bounds.is_null()) return;
Id tex_id = ctx.sprite_atlas.id;
return ctx.push_sprite(bounds, uv, tex_id)!;
return ctx.push_sprite(elem.bounds, uv, tex_id)!;
}

Loading…
Cancel
Save