Просмотр исходного кода

wallet: fractional scaling of UI

darkfi 2 лет назад
Родитель
Сommit
0a4a9645d8

+ 20 - 0
bin/darkwallet/gui/__init__.py

@@ -684,12 +684,32 @@ def draw():
 
     api.link_node(node_id, layer_id)
 
+class App2(EventLoop):
+
+    def key_down(self, keycode, keymods, repeat):
+        if repeat:
+            return
+
+        win_id = api.lookup_node_id("/window")
+        scale = api.get_property_value(win_id, "scale")[0]
+
+        if keymods.ctrl and keycode == "=":
+            scale *= 1.01
+        elif keymods.ctrl and keycode == "-":
+            scale *= 0.99
+
+        print(scale)
+        api.set_property_f32(win_id, "scale", 0, scale)
+
 def main():
     draw()
 
     # DEBUG
     print_tree()
 
+    app = App2()
+    app.run()
+
 #def main():
 #    if True:
 #        node_id = api.add_node("foo", SceneNodeType.WINDOW)

+ 29 - 28
bin/darkwallet/gui/api.py

@@ -1,5 +1,5 @@
 from collections import namedtuple
-from pydrk import Api, HostApi, PropertyType, PropertySubType, Property
+from pydrk import Api, HostApi, PropertyType, PropertySubType, Property, serial
 import zmq
 
 api = Api()
@@ -24,13 +24,13 @@ def remove_all_slots(node_path, sig):
         api.unregister_slot(node_id, sig, slot_id)
 
 def register_slot(node_path, sig, tag):
-    remove_all_slots(node_path, sig)
+    #remove_all_slots(node_path, sig)
     node_id = api.lookup_node_id(node_path)
     api.register_slot(node_id, sig, "", tag)
 
 def get_property(node_id, prop):
     node_id = lookup_node(node_id)
-    return api.get_property(node_id, prop)
+    return api.get_property_value(node_id, prop)
 
 def set_property(node_id, prop, val):
     node_id = lookup_node(node_id)
@@ -106,36 +106,37 @@ class EventLoop:
 
     def __init__(self):
         self.subsock = make_sub_socket()
-        register_slot("/window",                "resize",        b"rs")
-        register_slot("/window/input/mouse",    "button_down",   b"ck")
-        register_slot("/window/input/mouse",    "wheel",         b"wh")
-        register_slot("/window/input/mouse",    "move",          b"mm")
+        #register_slot("/window",                "resize",        b"rs")
+        #register_slot("/window/input/mouse",    "button_down",   b"ck")
+        #register_slot("/window/input/mouse",    "wheel",         b"wh")
+        #register_slot("/window/input/mouse",    "move",          b"mm")
         register_slot("/window/input/keyboard", "key_down",      b"kd")
 
     def run(self):
         while True:
-            data = self.subsock.recv()
-            match data:
-                case b"rs":
-                    w = get_property("/window", "width")
-                    h = get_property("/window", "height")
-                    self.resize_event(w, h)
-                case b"ck":
-                    x = get_property("/window/input/mouse", "click_x")
-                    y = get_property("/window/input/mouse", "click_y")
-                    self.mouse_click(x, y)
-                case b"wh":
-                    y = get_property("/window/input/mouse", "wheel_y")
-                    self.mouse_wheel(y)
-                case b"mm":
-                    pass
+            signal_data, user_data = self.subsock.recv_multipart()
+            cur = serial.Cursor(signal_data)
+            match user_data:
+                #case b"rs":
+                #    w = get_property("/window", "width")
+                #    h = get_property("/window", "height")
+                #    self.resize_event(w, h)
+                #case b"ck":
+                #    x = get_property("/window/input/mouse", "click_x")
+                #    y = get_property("/window/input/mouse", "click_y")
+                #    self.mouse_click(x, y)
+                #case b"wh":
+                #    y = get_property("/window/input/mouse", "wheel_y")
+                #    self.mouse_wheel(y)
+                #case b"mm":
+                #    pass
                 case b"kd":
-                    shift =   get_property("/window/input/keyboard", "shift")
-                    ctrl =    get_property("/window/input/keyboard", "ctrl")
-                    alt =     get_property("/window/input/keyboard", "alt")
-                    logo =    get_property("/window/input/keyboard", "logo")
-                    keycode = get_property("/window/input/keyboard", "keycode")
-                    repeat =  get_property("/window/input/keyboard", "repeat")
+                    shift = bool(serial.read_u8(cur))
+                    ctrl = bool(serial.read_u8(cur))
+                    alt = bool(serial.read_u8(cur))
+                    logo = bool(serial.read_u8(cur))
+                    repeat = bool(serial.read_u8(cur))
+                    keycode = serial.decode_str(cur)
 
                     keymods = KeyMods(shift, ctrl, alt, logo)
                     # Sometimes these get stuck when exiting the window.

+ 20 - 8
bin/darkwallet/src/chatview.rs

@@ -13,8 +13,12 @@ use crate::{error::{Error, Result}, prop::{
 
 fn read_lines<P>(filename: P) -> Vec<String>
 where P: AsRef<Path>, {
-    let file = File::open(filename).unwrap();
-    BufReader::new(file).lines().map(|l| l.unwrap()).collect()
+    //let file = File::open(filename).unwrap();
+    //BufReader::new(file).lines().map(|l| l.unwrap()).collect()
+    // Just so we can package for android easily
+    // Later this will be all replaced anyway
+    let file = include_bytes!("../chat.txt");
+    BufReader::new(&file[..]).lines().map(|l| l.unwrap()).collect()
 }
 
 pub type ChatViewPtr = Arc<ChatView>;
@@ -130,6 +134,14 @@ impl ChatView {
 
         render.ctx.apply_uniforms_from_bytes(uniforms_data.as_ptr(), uniforms_data.len());
 
+        // Used for scaling the font size
+        let window = render
+            .scene_graph
+            .lookup_node("/window")
+            .expect("no window attached!");
+        let window_scale = window.get_property_f32("scale")?;
+        let font_size = window_scale * 20.;
+
         let bound = Rectangle {
             x: 0.,
             y: 0.,
@@ -159,16 +171,16 @@ impl ChatView {
             };
 
             if glyph_line.is_empty() {
-                *glyph_line = self.text_shaper.shape(line.to_string(), 20., COLOR_WHITE);
+                *glyph_line = self.text_shaper.shape(line.to_string(), font_size, COLOR_WHITE);
             }
-            let linespacing = 30.;
+            let linespacing = window_scale*30.;
             let off_y = linespacing * i as f32 + scroll;
             if off_y + linespacing < 0. || off_y - linespacing > rect.h {
                 continue;
             }
 
             let times_color = [0.4, 0.4, 0.4, 1.];
-            let glyphs_time = self.text_shaper.shape(time.to_string(), 20., times_color);
+            let glyphs_time = self.text_shaper.shape(time.to_string(), font_size, times_color);
             let mut rhs = 0.;
             for glyph in glyphs_time {
                 let mut pos = glyph.pos.clone();
@@ -194,8 +206,8 @@ impl ChatView {
             ];
 
             let nick_color = nick_colors[nick.len() % nick_colors.len()];
-            let glyphs_nick = self.text_shaper.shape(nick.to_string(), 20., nick_color);
-            let off_x = rhs + 20.;
+            let glyphs_nick = self.text_shaper.shape(nick.to_string(), font_size, nick_color);
+            let off_x = rhs + window_scale*20.;
             for glyph in glyphs_nick {
                 let mut pos = glyph.pos.clone();
                 pos.x += off_x;
@@ -207,7 +219,7 @@ impl ChatView {
                 render.ctx.delete_texture(texture);
             }
 
-            let off_x = rhs + 20.;
+            let off_x = rhs + window_scale*20.;
             for glyph in glyph_line {
                 let mut pos = glyph.pos.clone();
                 pos.x += off_x;

+ 13 - 1
bin/darkwallet/src/editbox.rs

@@ -96,6 +96,7 @@ pub struct EditBox {
     text_shaper: TextShaper,
     key_repeat: Mutex<PressedKeysSmoothRepeat>,
     mouse_btn_held: AtomicBool,
+    window_scale: f32,
 }
 
 impl EditBox {
@@ -118,6 +119,14 @@ impl EditBox {
             font_faces
         };
 
+        // TODO: catch window resize event and regen glyphs
+        // Used for scaling the font size
+        let window = 
+            scene_graph
+            .lookup_node("/window")
+            .expect("no window attached!");
+        let window_scale = window.get_property_f32("scale")?;
+
         let self_ = Arc::new(Self{
             node_name: node_name.clone(),
             is_active,
@@ -136,6 +145,7 @@ impl EditBox {
             text_shaper,
             key_repeat: Mutex::new(PressedKeysSmoothRepeat::new(400, 50)),
             mouse_btn_held: AtomicBool::new(false),
+            window_scale,
         });
         self_.regen_glyphs().unwrap();
 
@@ -447,7 +457,9 @@ impl EditBox {
     }
 
     fn regen_glyphs(&self) -> Result<()> {
-        let glyphs = self.text_shaper.shape(self.text.get(), self.font_size.get(), 
+        let font_size = self.window_scale * self.font_size.get();
+
+        let glyphs = self.text_shaper.shape(self.text.get(), font_size,
                 self.text_color.get());
         if self.cursor_pos.get() > glyphs.len() as u32 {
             self.cursor_pos.set(glyphs.len() as u32);

+ 22 - 8
bin/darkwallet/src/gfx.rs

@@ -234,6 +234,10 @@ impl Stage {
         prop.set_f32(1, screen_height);
         window.add_property(prop).unwrap();
 
+        let mut prop = Property::new("scale", PropertyType::Float32, PropertySubType::Pixel);
+        prop.set_defaults_f32(vec![1.]).unwrap();
+        window.add_property(prop).unwrap();
+
         window
             .add_signal(
                 "resize",
@@ -524,11 +528,17 @@ impl<'a> RenderContext<'a> {
         self.ctx.apply_viewport(view.x, view.y, view.w, view.h);
         self.ctx.apply_scissor_rect(view.x, view.y, view.w, view.h);
 
+        let window = self
+            .scene_graph
+            .lookup_node("/window")
+            .expect("no window attached!");
+        let window_scale = window.get_property_f32("scale")?;
+
         let rect = Rectangle {
-            x: rect.x as f32,
-            y: rect.y as f32,
-            w: rect.w as f32,
-            h: rect.h as f32,
+            x: (rect.x as f32) / window_scale,
+            y: (rect.y as f32) / window_scale,
+            w: (rect.w as f32) / window_scale,
+            h: (rect.h as f32) / window_scale,
         };
 
         let layer_children =
@@ -725,7 +735,6 @@ impl<'a> RenderContext<'a> {
             Vertex { pos: [x2, y2], color, uv: [u2, v2] },
         ];
 
-        //debug!("screen size: {:?}", window::screen_size());
         let vertex_buffer = self.ctx.new_buffer(
             BufferType::VertexBuffer,
             BufferUsage::Immutable,
@@ -790,7 +799,6 @@ impl<'a> RenderContext<'a> {
             Vertex { pos: [x2, y2], color, uv: [u2, v2] },
         ];
 
-        //debug!("screen size: {:?}", window::screen_size());
         let vertex_buffer = self.ctx.new_buffer(
             BufferType::VertexBuffer,
             BufferUsage::Immutable,
@@ -831,7 +839,6 @@ impl<'a> RenderContext<'a> {
             Vertex { pos: [x2, y2], color, uv: [1., 1.] },
         ];
 
-        //debug!("screen size: {:?}", window::screen_size());
         let vertex_buffer = self.ctx.new_buffer(
             BufferType::VertexBuffer,
             BufferUsage::Immutable,
@@ -911,6 +918,14 @@ impl<'a> RenderContext<'a> {
 
         self.ctx.apply_uniforms_from_bytes(uniforms_data.as_ptr(), uniforms_data.len());
 
+        // Used for scaling the font size
+        let window = self
+            .scene_graph
+            .lookup_node("/window")
+            .expect("no window attached!");
+        let window_scale = window.get_property_f32("scale")?;
+        let font_size = window_scale * font_size;
+
         //let mut strings = vec![];
         //let mut current_str = String::new();
         //let mut current_idx = 0;
@@ -1126,7 +1141,6 @@ impl EventHandler for Stage {
     fn draw(&mut self) {
         self.last_draw_time = Some(Instant::now());
 
-        let (screen_width, screen_height) = window::screen_size();
         // This will make the top left (0, 0) and the bottom right (1, 1)
         // Default is (-1, 1) -> (1, -1)
         let proj = glam::Mat4::from_translation(glam::Vec3::new(-1., 1., 0.)) *