Remove the glade template and create rusty widgets

This commit is contained in:
Savanni D'Gerinel 2023-02-18 21:42:38 -05:00
parent 1016ba756c
commit 15b56e0c81
3 changed files with 91 additions and 125 deletions

View File

@ -1,99 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="HexGridWindow" parent="GtkApplicationWindow">
<property name="can-focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="spacing">8</property>
<child>
<object class="GtkDrawingArea" id="drawing_area">
<property name="width-request">1024</property>
<property name="height-request">768</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">8</property>
<property name="margin-end">8</property>
<property name="margin-top">8</property>
<property name="margin-bottom">8</property>
<property name="hexpand">True</property>
</object>
</child>
<child>
<object class="GtkBox">
<property name="width-request">100</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="orientation">vertical</property>
<property name="spacing">8</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="spacing">8</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Canvas Address</property>
</object>
</child>
<child>
<object class="GtkLabel" id="canvas_address">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="label" translatable="yes">-----</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="spacing">8</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Hexagon Address</property>
</object>
</child>
<child>
<object class="GtkLabel" id="hex_address">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="label" translatable="yes">-----</property>
</object>
</child>
</object>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/luminescent-dreams/hex-grid/">
<file>main.glade</file>
<file>terrain.ppm</file>
<file>map.txt</file>
</gresource>

View File

@ -110,15 +110,13 @@ fn main() {
app.run();
}
#[derive(CompositeTemplate, Default)]
#[template(resource = "/com/luminescent-dreams/hex-grid/main.glade")]
#[derive(Default)]
pub struct HexGridWindowPrivate {
#[template_child]
pub drawing_area: TemplateChild<DrawingArea>,
#[template_child]
pub hex_address: TemplateChild<Label>,
#[template_child]
pub canvas_address: TemplateChild<Label>,
layout: gtk::Box,
drawing_area: DrawingArea,
hex_address: Label,
canvas_address: Label,
current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
}
@ -129,12 +127,89 @@ impl ObjectSubclass for HexGridWindowPrivate {
type Type = HexGridWindow;
type ParentType = gtk::ApplicationWindow;
fn class_init(c: &mut Self::Class) {
c.bind_template();
}
fn new() -> Self {
let current_coordinate = Rc::new(RefCell::new(None));
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
let drawing_area = DrawingArea::builder()
.width_request(1024)
.height_request(768)
.margin_start(8)
.margin_end(8)
.margin_top(8)
.margin_bottom(8)
.hexpand(true)
.build();
let layout = gtk::Box::builder()
.homogeneous(false)
.spacing(8)
.can_focus(false)
.visible(true)
.build();
let sidebar = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.width_request(100)
.visible(true)
.can_focus(false)
.margin_start(4)
.margin_end(4)
.margin_top(4)
.margin_bottom(4)
.spacing(8)
.build();
let canvas_address_row = gtk::Box::builder()
.hexpand(true)
.spacing(8)
.homogeneous(true)
.build();
canvas_address_row.append(&gtk::Label::builder().label("Canvas Address").build());
let canvas_address = gtk::Label::builder()
.label("-----")
.margin_start(4)
.margin_end(4)
.margin_top(4)
.margin_bottom(4)
.build();
canvas_address_row.append(&canvas_address);
let hex_address_row = gtk::Box::builder()
.hexpand(true)
.spacing(8)
.homogeneous(true)
.build();
hex_address_row.append(&gtk::Label::builder().label("Hex Address").build());
let hex_address = gtk::Label::builder()
.label("-----")
.margin_start(4)
.margin_end(4)
.margin_top(4)
.margin_bottom(4)
.build();
hex_address_row.append(&hex_address);
sidebar.append(&canvas_address_row);
sidebar.append(&hex_address_row);
layout.append(&drawing_area);
layout.append(&sidebar);
layout.show();
Self {
drawing_area,
hex_address,
canvas_address,
current_coordinate,
layout,
}
}
}
@ -201,16 +276,6 @@ impl ObjectImpl for HexGridWindowPrivate {
context.set_line_width(2.);
/*
deep_water.render_on_context(&context, 0., 0.);
shallow_water.render_on_context(&context, 150., 0.);
grasslands.render_on_context(&context, 300., 0.);
desert.render_on_context(&context, 450., 0.);
mountain.render_on_context(&context, 0., 100.);
badlands.render_on_context(&context, 150., 100.);
swamp.render_on_context(&context, 0., 200.);
*/
for coordinate in vec![AxialAddr::origin()]
.into_iter()
.chain(AxialAddr::origin().addresses(MAP_RADIUS))
@ -235,7 +300,6 @@ impl ObjectImpl for HexGridWindowPrivate {
_ => panic!("unhandled terrain type"),
};
tile.render_on_context(context, translate_x, translate_y);
// draw_hexagon(context, center_x, center_y, HEX_RADIUS);
}
});
}
@ -254,7 +318,9 @@ glib::wrapper! {
impl HexGridWindow {
pub fn new(app: &Application) -> Self {
Object::builder().property("application", app).build()
let window: Self = Object::builder().property("application", app).build();
window.set_child(Some(&window.imp().layout));
window
}
}