Skip to content

Commit

Permalink
Store window coordinates & corrected 80x50 font.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed May 7, 2024
1 parent 253dde3 commit fff5d52
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
5 changes: 5 additions & 0 deletions crates/icy_engine/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ impl Buffer {
let size = size.into();
self.size = size;
}

pub fn set_default_size(&mut self, size: impl Into<Size>) {
let size = size.into();
self.original_size = size;
}

pub fn set_width(&mut self, width: i32) {
self.size.width = width;
Expand Down
21 changes: 20 additions & 1 deletion crates/icy_term/src/data/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::Duration,
};

use egui::Modifiers;
use egui::{Modifiers, Rect};
use egui_bind::KeyOrPointer;
use i18n_embed_fl::fl;
use icy_engine::Color;
Expand Down Expand Up @@ -284,6 +284,8 @@ pub struct Options {
pub bind: KeyBindings,
pub iemsi: IEMSISettings,

pub window_rect: Option<Rect>,

pub modem: Modem,
}

Expand All @@ -297,6 +299,7 @@ impl Default for Options {
console_beep: true,
bind: KeyBindings::default(),
is_dark_mode: None,
window_rect: None,
modem: Modem::default(),
}
}
Expand Down Expand Up @@ -341,6 +344,10 @@ impl Options {
let mut file = File::create(&write_name)?;
file.write_all(b"version = \"1.1\"\n")?;

if let Some(rect) = self.window_rect {
file.write_all(format!("window_coord = \"{}/{}-{}/{}\"\n", rect.min.x, rect.min.y, rect.max.x, rect.max.y).as_bytes())?;
}

file.write_all(format!("scaling = \"{:?}\"\n", self.scaling).as_bytes())?;
if let Some(dark_mode) = self.is_dark_mode {
file.write_all(format!("is_dark_mode = {dark_mode}\n").as_bytes())?;
Expand Down Expand Up @@ -436,6 +443,18 @@ fn parse_value(options: &mut Options, value: &Value) {
Value::Table(table) => {
for (k, v) in table {
match k.as_str() {
"window_coord" => {
if let Value::String(str) = v {
let mut minmax = str.split('-');
let min = minmax.next().unwrap().split('/').collect::<Vec<&str>>();
let max = minmax.next().unwrap().split('/').collect::<Vec<&str>>();

options.window_rect = Some(Rect::from_min_max(
egui::Pos2::new(min[0].parse::<f32>().unwrap(), min[1].parse::<f32>().unwrap()),
egui::Pos2::new(max[0].parse::<f32>().unwrap(), max[1].parse::<f32>().unwrap()),
));
}
}
"scaling" => {
if let Value::String(str) = v {
match str.as_str() {
Expand Down
20 changes: 18 additions & 2 deletions crates/icy_term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,29 @@ fn main() {

use eframe::icon_data::from_png_bytes;

let options = match Options::load_options() {
Ok(options) => options,
Err(e) => {
log::error!("Error reading dialing_directory: {e}");
Options::default()
}
};

let mut native_options = eframe::NativeOptions {
multisampling: 0,
renderer: eframe::Renderer::Glow,
..Default::default()
};
let icon_data = from_png_bytes(include_bytes!("../build/linux/256x256.png")).unwrap();
native_options.viewport = native_options.viewport.with_inner_size(egui::vec2(1284. + 8., 839.)).with_icon(icon_data);
if let Some(rect) = options.window_rect {
native_options.viewport = native_options
.viewport
.with_inner_size(rect.max.to_vec2())
.with_icon(icon_data)
.with_position(rect.min);
} else {
native_options.viewport = native_options.viewport.with_inner_size(egui::vec2(1284. + 8., 839.)).with_icon(icon_data);
}

if let Ok(log_file) = get_log_file() {
// delete log file when it is too big
Expand Down Expand Up @@ -158,7 +174,7 @@ fn main() {

log::info!("Starting iCY TERM {}", *VERSION);

if let Err(err) = eframe::run_native(&DEFAULT_TITLE, native_options, Box::new(|cc| Box::new(MainWindow::new(cc)))) {
if let Err(err) = eframe::run_native(&DEFAULT_TITLE, native_options, Box::new(|cc| Box::new(MainWindow::new(cc, options)))) {
log::error!("Error returned by run_native: {}", err);
}
log::info!("shutting down.");
Expand Down
28 changes: 17 additions & 11 deletions crates/icy_term/src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{path::PathBuf, sync::Arc, time::Duration};

use directories::UserDirs;
use eframe::egui::{self};
use egui::{mutex::Mutex, FontId};
use egui::{mutex::Mutex, FontId, Rect};
use icy_engine::Position;
use icy_net::{
protocol::TransferState,
Expand All @@ -24,20 +24,12 @@ use crate::{
use super::{MainWindow, MainWindowMode};

impl MainWindow {
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
pub fn new(cc: &eframe::CreationContext<'_>, options: Options) -> Self {
use egui::FontFamily::Proportional;
use egui::TextStyle::{Body, Button, Heading, Monospace, Small};
egui_extras::install_image_loaders(&cc.egui_ctx);

let gl = cc.gl.as_ref().expect("You need to run eframe with the glow backend");
let options = match Options::load_options() {
Ok(options) => options,
Err(e) => {
log::error!("Error reading dialing_directory: {e}");
Options::default()
}
};

let mut view = BufferView::new(gl);
view.interactive = true;
view.get_edit_state_mut().set_unicode_converter(get_unicode_converter(&TerminalEmulation::Ansi));
Expand Down Expand Up @@ -148,7 +140,6 @@ impl MainWindow {
]
.into();
ctx.set_style(style);

view
}
}
Expand All @@ -167,6 +158,21 @@ impl eframe::App for MainWindow {
#[cfg(not(target_arch = "wasm32"))]
self.update_title(ctx);

ctx.input(|i| {
if let Some(or) = i.viewport().outer_rect {
if let Some(ir) = i.viewport().inner_rect {
let rect = Rect {
min: or.min,
max: (ir.max - ir.min).to_pos2(),
};
if self.state.options.window_rect != Some(rect) {
self.state.options.window_rect = Some(rect);
self.state.store_options();
}
}
}
});

match self.get_mode() {
MainWindowMode::ShowTerminal => {
self.handle_terminal_key_binds(ctx);
Expand Down
16 changes: 14 additions & 2 deletions crates/icy_term/src/ui/util/screen_modes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt::Display;

use icy_engine::{BitFont, Color, Palette, Size, ATARI, ATARI_DEFAULT_PALETTE, C64_DEFAULT_PALETTE, C64_LOWER, C64_UPPER, CP437, VIEWDATA, VIEWDATA_PALETTE};
use icy_engine::{
BitFont, Color, Palette, Size, ATARI, ATARI_DEFAULT_PALETTE, C64_DEFAULT_PALETTE, C64_LOWER, C64_UPPER, CP437, IBM_VGA50_SAUCE, VIEWDATA, VIEWDATA_PALETTE,
};
use icy_engine_gui::BufferInputMode;

use crate::ui::MainWindow;
Expand Down Expand Up @@ -100,11 +102,21 @@ impl ScreenMode {
}

pub fn set_mode(&self, main_window: &MainWindow) {
main_window.buffer_view.lock().get_buffer_mut().set_default_size(self.get_window_size());
main_window.buffer_view.lock().get_buffer_mut().set_size(self.get_window_size());
main_window.buffer_view.lock().get_buffer_mut().terminal_state.set_size(self.get_window_size());
match self {
// ScreenMode::Cga(_, h) | ScreenMode::Ega(_, h) |
ScreenMode::Vga(_, _) | ScreenMode::Default => {
ScreenMode::Vga(_x, y) => {
main_window.buffer_view.lock().get_buffer_mut().clear_font_table();
main_window
.buffer_view
.lock()
.get_buffer_mut()
.set_font(0, BitFont::from_bytes("", if *y >= 50 { IBM_VGA50_SAUCE } else { CP437 }).unwrap());
main_window.buffer_view.lock().get_buffer_mut().palette = Palette::dos_default();
}
ScreenMode::Default => {
main_window.buffer_view.lock().get_buffer_mut().clear_font_table();
main_window
.buffer_view
Expand Down

0 comments on commit fff5d52

Please sign in to comment.