Skip to content

Commit

Permalink
Unified server and client drawing schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Feb 14, 2024
1 parent ce458ec commit 9e0ab47
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 192 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion StarterProject.eldiron

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions creator/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl TheTrait for Editor {
}
}
if self.server.state == ServerState::Running {
self.client.tick();
let debug = self.server.tick();
if !debug.is_empty() {
self.sidebar.add_debug_messages(debug, ui, ctx);
Expand Down Expand Up @@ -824,6 +825,7 @@ impl TheTrait for Editor {
));
update_server_icons = true;
} else if self.server.state == ServerState::Paused {
self.client.tick();
let debug = self.server.tick();
if !debug.is_empty() {
self.sidebar.add_debug_messages(debug, ui, ctx);
Expand Down
6 changes: 4 additions & 2 deletions creator/src/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ impl Panels {
redraw = true;
}
}
} else if id.name == "Set CodeGrid Panel" {
}
else if id.name == "Set CodeGrid Panel" {
//println!("Set CodeGrid Panel");
ctx.ui
.send(TheEvent::SetStackIndex(TheId::named("Left Stack"), 1));
Expand All @@ -351,7 +352,8 @@ impl Panels {
// redraw = true;
// }
// }
} else if id.name == "Set Tilemap Panel" {
}
else if id.name == "Set Tilemap Panel" {
//println!("Set Tilemap Panel");
ctx.ui
.send(TheEvent::SetStackIndex(TheId::named("Left Stack"), 2));
Expand Down
1 change: 1 addition & 0 deletions creator/src/screeneditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl ScreenEditor {
let mut screen_editor = TheRGBALayout::new(TheId::named("Screen Editor"));
if let Some(rgba_view) = screen_editor.rgba_view_mut().as_rgba_view() {
rgba_view.set_mode(TheRGBAViewMode::Display);
rgba_view.set_dont_show_grid(true);

if let Some(buffer) = ctx.ui.icon("eldiron_map") {
rgba_view.set_buffer(buffer.clone());
Expand Down
35 changes: 26 additions & 9 deletions creator/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,13 +1475,15 @@ impl Sidebar {
))
}
}
} else if id.name == "Screen Item" {
}
else if id.name == "Screen Item" {
if let Some(s) = project.screens.get(&id.uuid) {
self.apply_screen(ui, ctx, Some(s));
server_ctx.curr_screen = s.id;
redraw = true;
}
} else if id.name == "Screen Add" {
}
else if id.name == "Screen Add" {
if let Some(list_layout) = ui.get_list_layout("Screen List") {
let screen = Screen::default();

Expand Down Expand Up @@ -1636,7 +1638,8 @@ impl Sidebar {
));
self.deselect_sections_buttons(ui, id.name.clone());
redraw = true;
} else if id.name == "Module Section" && *state == TheWidgetState::Selected {
}
else if id.name == "Module Section" && *state == TheWidgetState::Selected {
self.deselect_sections_buttons(ui, id.name.clone());
CODEEDITOR.lock().unwrap().set_allow_modules(false);
set_server_externals();
Expand Down Expand Up @@ -1667,7 +1670,8 @@ impl Sidebar {
SidebarMode::Module as usize,
));
redraw = true;
} else if id.name == "Screen Section" && *state == TheWidgetState::Selected {
}
else if id.name == "Screen Section" && *state == TheWidgetState::Selected {
self.deselect_sections_buttons(ui, id.name.clone());
CODEEDITOR.lock().unwrap().set_allow_modules(true);
set_client_externals();
Expand Down Expand Up @@ -1698,7 +1702,8 @@ impl Sidebar {
SidebarMode::Screen as usize,
));
redraw = true;
} else if id.name == "Asset Section" && *state == TheWidgetState::Selected {
}
else if id.name == "Asset Section" && *state == TheWidgetState::Selected {
self.deselect_sections_buttons(ui, id.name.clone());

if let Some(widget) = ui
Expand Down Expand Up @@ -1727,7 +1732,8 @@ impl Sidebar {
SidebarMode::Asset as usize,
));
redraw = true;
} else if id.name == "Debug Section" && *state == TheWidgetState::Selected {
}
else if id.name == "Debug Section" && *state == TheWidgetState::Selected {
self.deselect_sections_buttons(ui, id.name.clone());

if let Some(widget) = ui
Expand All @@ -1742,7 +1748,8 @@ impl Sidebar {
ctx.ui
.send(TheEvent::SetStackIndex(self.stack_layout_id.clone(), 5));
redraw = true;
} else if id.name == "Thanks Section" && *state == TheWidgetState::Selected {
}
else if id.name == "Thanks Section" && *state == TheWidgetState::Selected {
self.deselect_sections_buttons(ui, id.name.clone());
CODEEDITOR.lock().unwrap().set_allow_modules(false);

Expand All @@ -1758,7 +1765,8 @@ impl Sidebar {
ctx.ui
.send(TheEvent::SetStackIndex(self.stack_layout_id.clone(), 6));
redraw = true;
} else if id.name == "Compile" {
}
else if id.name == "Compile" {
// Compile button in the editor. Compile the code and send it to the server if successful.
// We do not need to store it in the project because thats already done in the
// CodeBundleChanged event.
Expand Down Expand Up @@ -2402,6 +2410,15 @@ impl Sidebar {
}
}
}

// Activate the current widget
if let Some(selected) = list.selected() {
ctx.ui
.send(TheEvent::StateChanged(selected, TheWidgetState::Selected));
}
else {
list.select_first_item(ctx);
}
}

ctx.ui.relayout = true;
Expand Down Expand Up @@ -2784,7 +2801,7 @@ impl Sidebar {
project: &mut Project,
server: &mut Server,
) {
TILEDRAWER.lock().unwrap().tiles = project.extract_tiles();
TILEDRAWER.lock().unwrap().set_tiles(project.extract_tiles());
server.update_tiles(project.extract_tiles());
if let Some(widget) = ui.get_widget("RenderView") {
if let Some(w) = widget
Expand Down
2 changes: 1 addition & 1 deletion creator/src/tileeditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl TileEditor {
}

pub fn load_from_project(&mut self, ui: &mut TheUI, _ctx: &mut TheContext, project: &Project) {
TILEDRAWER.lock().unwrap().tiles = project.extract_tiles();
TILEDRAWER.lock().unwrap().set_tiles(project.extract_tiles());
if let Some(widget) = ui.get_widget("RenderView") {
if let Some(w) = widget
.as_any()
Expand Down
21 changes: 9 additions & 12 deletions shared/src/client/functions.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
//use crate::prelude::*;
use super::{CHARACTER, REGIONS, TILEDRAWER, UPDATE, WIDGETBUFFER, FONTS};
use super::{CHARACTER, REGIONS, TILEDRAWER, UPDATE, WIDGETBUFFER, FONTS, DRAWSETTINGS};
use theframework::prelude::*;

pub fn add_compiler_client_functions(compiler: &mut TheCompiler) {
//
compiler.add_external_call(
"DrGame".to_string(),
|_stack, _data, _sandbox| {

let mut buffer = WIDGETBUFFER.write().unwrap();
let mut update = UPDATE.write().unwrap();
let tiledrawer = TILEDRAWER.read().unwrap();

if let Some(region) = REGIONS.read().unwrap().get(&update.id) {
let mut offset = Vec2i::zero();
if let Some(character) = update.characters.get(&CHARACTER.read().unwrap()) {
offset.x = character.position.x as i32 * region.grid_size;
offset.y = character.position.y as i32 * region.grid_size;
let mut settings = DRAWSETTINGS.write().unwrap();
let character_id = CHARACTER.read().unwrap();
if let Some(_character) = update.characters.get(&character_id) {
settings.center_on_character = Some(*character_id);
} else {
settings.center_on_character = None;
}
tiledrawer.draw_region(&mut buffer, region, &0, &mut update, &16.0, &0, offset);
tiledrawer.draw_region(&mut buffer, region, &mut update, &settings);
}

// if let Some(key) = KEY_DOWN.read().unwrap().clone() {
// stack.push(TheValue::Text(key));
// } else {
// stack.push(TheValue::Empty);
// }

TheCodeNodeCallResult::Continue
},
vec![],
Expand Down
38 changes: 35 additions & 3 deletions shared/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lazy_static! {

pub static ref IMAGES: RwLock<FxHashMap<String, TheRGBABuffer>> = RwLock::new(FxHashMap::default());
pub static ref FONTS: RwLock<FxHashMap<String, fontdue::Font>> = RwLock::new(FxHashMap::default());
pub static ref DRAWSETTINGS: RwLock<RegionDrawSettings> = RwLock::new(RegionDrawSettings::new());
}

#[derive()]
Expand All @@ -27,6 +28,11 @@ pub struct Client {
widgets: FxHashMap<Uuid, TheCodePackage>,

compiler: TheCompiler,

redraw_ms: u32,
tick_ms: u32,

last_tick: i64,
}

impl Default for Client {
Expand All @@ -47,12 +53,17 @@ impl Client {
widgets: FxHashMap::default(),

compiler,

redraw_ms: 1000 / 30,
tick_ms: 250,

last_tick: 0,
}
}

/// Sets the project
pub fn set_project(&mut self, project: Project) {
TILEDRAWER.write().unwrap().tiles = project.extract_tiles();
TILEDRAWER.write().unwrap().set_tiles(project.extract_tiles());

let mut regions = REGIONS.write().unwrap();

Expand All @@ -68,6 +79,10 @@ impl Client {
}

self.set_assets(project.assets.clone());

self.tick_ms = project.tick_ms;
self.redraw_ms = 1000 / project.target_fps;
DRAWSETTINGS.write().unwrap().delta = self.redraw_ms as f32 / self.tick_ms as f32;
self.project = project;
}

Expand Down Expand Up @@ -114,8 +129,14 @@ impl Client {
*CHARACTER.write().unwrap() = character_id;
}

pub fn set_region_update(&mut self, region_update: RegionUpdate) {
*UPDATE.write().unwrap() = region_update;
pub fn set_region_update(&mut self, json: String) {
if let Some(update) = RegionUpdate::from_json(&json) {
*UPDATE.write().unwrap() = update;
}
}

pub fn tick(&mut self) {
DRAWSETTINGS.write().unwrap().anim_counter += 1;
}

pub fn draw_screen(
Expand All @@ -126,6 +147,17 @@ impl Client {
_ctx: &mut TheContext,
_server_ctx: &ServerContext,
) {
let delta = self.redraw_ms as f32 / self.tick_ms as f32;

let server_tick = UPDATE.read().unwrap().server_tick;

if server_tick != self.last_tick {
DRAWSETTINGS.write().unwrap().delta_in_tick = 0.0;
self.last_tick = server_tick;
} else {
DRAWSETTINGS.write().unwrap().delta_in_tick += delta;
}

buffer.fill(BLACK);

if let Some(screen) = self.project.screens.get(uuid) {
Expand Down
2 changes: 1 addition & 1 deletion shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod prelude {
pub use crate::server::context::ServerContext;
pub use crate::server::{Server, ServerState};
pub use crate::tilearea::TileArea;
pub use crate::tiledrawer::TileDrawer;
pub use crate::tiledrawer::{TileDrawer, RegionDrawSettings};
pub use crate::tilemap::{Tile, TileRole, Tilemap};
pub use crate::update::*;
pub use crate::widget::*;
Expand Down
4 changes: 2 additions & 2 deletions shared/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ impl Server {
}

/// Get the update for the given region.
pub fn get_region_update(&self, region_id: Uuid) -> Option<RegionUpdate> {
UPDATES.read().unwrap().get(&region_id).cloned()
pub fn get_region_update(&self, region_id: Uuid) -> Option<String> {
UPDATES.read().unwrap().get(&region_id).map(|update| update.to_json())
}

/// Sets the currently pressed key.
Expand Down
Loading

0 comments on commit 9e0ab47

Please sign in to comment.