added glScissor functionality; cleanup
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 10 Feb 2022 01:10:49 +0000 (19:10 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 10 Feb 2022 01:10:49 +0000 (19:10 -0600)
src/entity/editor.onyx
src/entity/player.onyx
src/gfx/immediate.onyx
src/gfx/ui.onyx
src/main.onyx

index 3cdb998e2fe2c253965663d109ada89c76919c6f..0450ae28ba4b1caea8b697b0824a0b6f5d38c6fa 100644 (file)
@@ -30,7 +30,7 @@ editor_toggle :: () {
 
 editor_update :: (dt: f32) {
     move_towards(^editor_openness, editor_target_openness, dt * 6);
-    
+
     if is_key_just_up(GLFW_KEY_1) do clicked_tab = .Create;
     if is_key_just_up(GLFW_KEY_2) do clicked_tab = .Edit;
 
@@ -64,7 +64,7 @@ editor_update :: (dt: f32) {
 #local handle_entity_selction_and_dragging :: (dt: f32) {
     mouse_pos := mouse_get_position_vector();
 
-    if mouse_pos.x < ~~window_width - sidebar_width * 2 && mouse_pos.y > menubar_height {
+    if mouse_pos.x < ~~window_width - sidebar_width && mouse_pos.y > menubar_height {
         if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) || is_button_just_down(GLFW_MOUSE_BUTTON_RIGHT) {
             if active_tab == .Edit do active_index = -1;
             selected_entity_id = Entity_Nothing;
@@ -78,7 +78,7 @@ editor_update :: (dt: f32) {
                     break;
                 }
             }
-            
+
             dragging = is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) && selected_entity_id != Entity_Nothing;
             resizing = is_button_just_down(GLFW_MOUSE_BUTTON_RIGHT) && selected_entity_id != Entity_Nothing;
         }
@@ -154,7 +154,7 @@ editor_draw :: () {
 
     { // Draw sidebar, if necessary
         sidebar_width = editor_openness * 400.0f;
-        w := sidebar_width; 
+        w := sidebar_width;
         x := ~~ window_width - w;
         y := 40.0f;
         h := ~~ window_height - y;
@@ -199,7 +199,7 @@ editor_draw :: () {
         test << #char "t";
     }
 
-    draw_textbox(.{x, y + 72.0f, w, 36.0f}, ^test);
+    draw_textbox(.{x, y + 72.0f, w / 2.0f, 36.0f}, ^test);
 
     #persist dumb := false;
 
@@ -213,11 +213,12 @@ editor_draw :: () {
 
     font_print(editor_big_font, x + 2, y + 24, info.name);
 
+    if active_index >= 0 do sidebar_width += w;
     render_struct_fields(any.{~~entity, entity.type}, 0, x, y + 4, w, h);
 }
 
 #local render_struct_fields :: (v: any, i: i32, x, y, w, h: f32, depth := 0) -> (new_y: f32, new_i: i32) {
-    Field_Height :: 22.0f
+    Field_Height :: 22.0f;
 
     info := cast(^type_info.Type_Info_Struct) type_info.get_type_info(v.type);
     for^ info.members {
@@ -228,19 +229,20 @@ editor_draw :: () {
 
         if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) {
             if Rect.contains(.{x, y + 2, w, Field_Height + 2}, mouse_get_position_vector()) {
+                if active_index < 0 do sidebar_width += w;
                 active_index = i - 1;
             }
         }
 
         if active_index + 1 == i {
-           immediate_set_color(.{.4,.4,.2});
-           immediate_rectangle(x, y + 2, w, Field_Height + 2);
+            immediate_set_color(.{.4,.4,.2});
+            immediate_rectangle(x, y + 2, w, Field_Height + 2);
 
-           render_field_editor(member_any, y, it.name);
+            render_field_editor(member_any, y, it.name);
 
         } elseif i % 2 == 0 {
-           immediate_set_color(.{.3,.3,.3});
-           immediate_rectangle(x, y + 2, w, Field_Height + 2);
+            immediate_set_color(.{.3,.3,.3});
+            immediate_rectangle(x, y + 2, w, Field_Height + 2);
         }
 
         font_print(editor_font, x + 2, y + Field_Height, it.name);
@@ -275,9 +277,9 @@ editor_draw :: () {
 }
 
 #local render_field_editor :: (v: any, y: f32, field_name: str) {
-    w := sidebar_width;
+    w := sidebar_width / 2;
     h := 200.0f;
-    x := ~~ window_width - sidebar_width * 2;
+    x := ~~ window_width - sidebar_width;
     y = math.min(y, ~~ window_height - h);
     immediate_set_color(.{.3,.3,.3});
     immediate_rectangle(x, y, w, h);
index c59d482ee0106cceadf86e45eeef316798533e6e..9592a30d78a39772097e3d2840f7f11cac48c5ba 100644 (file)
@@ -76,10 +76,10 @@ Player :: struct {
         speed :: 128.0f;
 
         delta: Vector2;
-        if is_key_down(controls.left)  { delta.x -= speed * dt; facing = .Left;  } 
-        if is_key_down(controls.right) { delta.x += speed * dt; facing = .Right; } 
-        if is_key_down(controls.up)    { delta.y -= speed * dt; facing = .Up;    } 
-        if is_key_down(controls.down)  { delta.y += speed * dt; facing = .Down;  } 
+        if is_key_down(controls.left)  { delta.x -= speed * dt; facing = .Left;  }
+        if is_key_down(controls.right) { delta.x += speed * dt; facing = .Right; }
+        if is_key_down(controls.up)    { delta.y -= speed * dt; facing = .Up;    }
+        if is_key_down(controls.down)  { delta.y += speed * dt; facing = .Down;  }
 
         dist := math.max(size.x, size.y) * 2;
         walls := scene->query_by_flags(.{pos.x - dist, pos.y - dist, dist * 2, dist * 2}, .Solid);
@@ -298,4 +298,3 @@ move_towards :: (v: ^$T, target: T, diff: T) {
     if *v < target do *v += diff;
     if *v > target do *v -= diff;
 }
-
index b15825a49537d7ae82412549ecc97996dab11e6e..231b97d4b21cf3cb5b5ae7922f336b8b720d987e 100644 (file)
@@ -112,6 +112,28 @@ immediate_subimage :: (image: ^Texture, x, y, w, h: f32, tx, ty, tw, th: f32) {
 
 immediate_ellipse :: () {}
 
+immediate_push_scissor :: (x, y, w, h: f32) {
+    // Assuming that x, y, w, and h are in screen (window) coordinates.
+
+    scissors << .{x, y, w, h};
+    glEnable(GL_SCISSOR_TEST);
+    glScissor(~~x, window_height - ~~(y + h), ~~w, ~~h);
+}
+
+immediate_pop_scissor :: () {
+    if scissors.count > 0 {
+        array.pop(^scissors);
+    }
+
+    if scissors.count > 0 {
+        glEnable(GL_SCISSOR_TEST);
+        s := scissors[scissors.count - 1];
+        glScissor(~~s.x, window_height - ~~(s.y + s.h), ~~s.w, ~~s.h);
+    } else {
+        glDisable(GL_SCISSOR_TEST);
+    }
+}
+
 Color :: struct {
     r, g, b :  f32;
     a       := 1.0f;
@@ -141,6 +163,11 @@ Immediate_Vertex :: struct {
     }
     rendering_type := Rendering_Type.Plain;
 
+    Scissor :: struct {
+        x, y, w, h: f32;
+    }
+    scissors: [..] Scissor;
+
     set_rendering_type :: (new_type: typeof rendering_type) {
         if rendering_type != new_type {
             immediate_flush();
index 62871bdff4ecb3a0fb6becd5cbdfc51cb84308a4..112706b0ec88c6be9f5a076752a6f7774c59f211 100644 (file)
@@ -147,7 +147,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th
     border_width := theme.border_width;
     text_color := theme.text_color;
     text := str.{text_buffer.data, text_buffer.count};
-    if text.count == 0 && placeholder.count >0 {
+    if text.count == 0 && placeholder.count > 0 {
         text = placeholder;
         text_color = theme.placeholder_text_color;
     }
@@ -205,7 +205,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th
                     }
 
                     case GLFW_KEY_DELETE {
-                        array.delete(text_buffer, 0);
+                        array.delete(text_buffer, textbox_editing_state.cursor_position);
                     }
 
                     case #default {
@@ -230,7 +230,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th
         move_towards(^animation_state.hover_time, 0.0f, theme.hover_speed);
     }
 
-    // immediate_push_scissor(x, y, w, h);    
+    immediate_push_scissor(x, y, w, h);
     immediate_set_color(theme.border_color);
     immediate_rectangle(x, y, w, h);
 
@@ -244,7 +244,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th
 
     move_towards(^animation_state.click_time, 0.0f, theme.click_decay_speed);
 
-    // immediate_pop_scissor();
+    immediate_pop_scissor();
     return result;
 }
 
@@ -436,4 +436,4 @@ draw_checkbox :: (use r: Rect, value: ^bool, text: str, theme := ^default_checkb
         b = c1.b * (1 - t) + c2.b * t,
         a = c1.a * (1 - t) + c2.a * t,
     };
-}
\ No newline at end of file
+}
index e7a80db56ec9c4907a248d597272de8240348ac6..ad85974a2c14f99b185c9cd791ebab1fef8cf1c4 100644 (file)
@@ -94,7 +94,7 @@ draw :: () {
     defer input_post_update();
     defer {
         immediate_flush();
-        
+
         #if DEBUG && false {
             font_set_color(.{1,0,0});
             font_print(debug_font, 0, 16, "FPS: {}", game_fps);