Skip to content

Commit

Permalink
editor: barebones theme tab
Browse files Browse the repository at this point in the history
  • Loading branch information
barsoosayque committed Nov 24, 2024
1 parent 5ab3acd commit ab66b11
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 137 deletions.
56 changes: 56 additions & 0 deletions crates/opensi-editor/src/card.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use opensi_core::prelude::*;

#[derive(Debug, Clone, Copy)]
pub enum CardKind<'a> {
Theme(&'a Theme),
Question(&'a Question),
New,
}

impl<'a> CardKind<'a> {
pub fn show(&self, ui: &mut egui::Ui) -> egui::Response {
let (text, fill, text_color) = match self {
CardKind::Theme(theme) => (
theme.name.clone(),
ui.visuals().widgets.active.bg_fill,
ui.visuals().widgets.active.text_color(),
),
CardKind::Question(question) => (
question.price.to_string(),
egui::Color32::TRANSPARENT,
ui.visuals().widgets.inactive.text_color(),
),
CardKind::New => (
"➕ Новый вопрос".to_string(),
egui::Color32::TRANSPARENT,
ui.visuals().weak_text_color(),
),
};
let mut frame = egui::Frame::default()
.inner_margin(16.0)
.outer_margin(egui::Margin::symmetric(0.0, 4.0))
.stroke(ui.style().visuals.widgets.noninteractive.bg_stroke)
.rounding(8.0)
.fill(fill)
.begin(ui);

// aprox
let font_size = 22.0 - (text.len() as isize - 8).max(0) as f32 * 0.3;
frame.content_ui.add_sized(
egui::vec2(100.0, 0.0),
egui::Label::new(egui::RichText::new(&text).size(font_size).color(text_color))
.selectable(false)
.halign(egui::Align::Center)
.wrap(),
);
let rect =
frame.content_ui.min_rect() + frame.frame.inner_margin + frame.frame.outer_margin;
let response = ui.allocate_rect(rect, egui::Sense::click());
if response.hovered() {
frame.frame.stroke = ui.style().visuals.widgets.active.bg_stroke;
frame.frame.fill = ui.style().visuals.widgets.active.weak_bg_fill;
}
frame.paint(ui);
response.on_hover_text(&text)
}
}
1 change: 1 addition & 0 deletions crates/opensi-editor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![warn(clippy::all)]

mod app;
mod card;
mod file_dialogs;
mod package_tab;
mod package_tree;
Expand Down
31 changes: 9 additions & 22 deletions crates/opensi-editor/src/package_tab.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::Itertools;
use opensi_core::prelude::*;

use crate::utils::{danger_button, unselectable_heading, unselectable_label};
use crate::utils::{danger_button, simple_row, unselectable_heading, unselectable_label};

/// Workarea tab to edit package info.
pub fn package_tab(package: &mut Package, selected: &mut Option<PackageNode>, ui: &mut egui::Ui) {
Expand Down Expand Up @@ -43,22 +43,22 @@ fn package_info_edit(package: &mut Package, ui: &mut egui::Ui) {
.cell_layout(egui::Layout::left_to_right(egui::Align::Min))
.striped(false)
.body(|mut body| {
package_edit_row("Название", &mut body, |ui| {
simple_row("Название", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut package.name);
});
package_edit_row("Сложность", &mut body, |ui| {
simple_row("Сложность", 20.0, &mut body, |ui| {
ui.add(egui::DragValue::new(&mut package.difficulty).range(0..=10));
});
package_edit_row("Ограничения", &mut body, |ui| {
simple_row("Ограничения", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut package.restriction);
});
package_edit_row("Дата создания", &mut body, |ui| {
simple_row("Дата создания", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut package.date);
});
package_edit_row("Издатель", &mut body, |ui| {
simple_row("Издатель", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut package.publisher);
});
package_edit_row("Язык", &mut body, |ui| {
simple_row("Язык", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut package.language);
});
});
Expand All @@ -73,28 +73,15 @@ fn package_metadata_edit(package: &Package, ui: &mut egui::Ui) {
.cell_layout(egui::Layout::left_to_right(egui::Align::Min))
.striped(true)
.body(|mut body| {
package_edit_row("ID пакета", &mut body, |ui| {
simple_row("ID пакета", 20.0, &mut body, |ui| {
ui.label(&package.id);
});
package_edit_row("Версия пакета", &mut body, |ui| {
simple_row("Версия пакета", 20.0, &mut body, |ui| {
ui.label(format!("{:.1}", package.version));
});
});
}

fn package_edit_row(
label: impl AsRef<str>,
body: &mut egui_extras::TableBody,
content: impl FnOnce(&mut egui::Ui),
) {
body.row(20.0, |mut row| {
row.col(|ui| {
ui.label(label.as_ref());
});
row.col(content);
});
}

fn package_rounds(package: &mut Package, selected: &mut Option<PackageNode>, ui: &mut egui::Ui) {
fn round_card(round: &Round, ui: &mut egui::Ui) -> egui::Response {
let mut frame = egui::Frame::default()
Expand Down
116 changes: 10 additions & 106 deletions crates/opensi-editor/src/round_tab.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use opensi_core::prelude::*;

use crate::utils::{error_label, string_list, unselectable_heading, unselectable_label};
use crate::{
card::CardKind,
utils::{error_label, info_edit, simple_row, unselectable_heading, unselectable_label},
};

/// Workarea tab to edit round info and its themes.
pub fn round_tab(
Expand Down Expand Up @@ -33,7 +36,7 @@ pub fn round_tab(
strip.cell(|ui| {
unselectable_heading("Дополнительная информация", ui);
ui.separator();
round_info_edit(&mut round.info, ui);
info_edit(&mut round.info, ui);
});
});
});
Expand All @@ -52,119 +55,22 @@ fn round_edit(round: &mut Round, ui: &mut egui::Ui) {
.cell_layout(egui::Layout::left_to_right(egui::Align::Min))
.striped(false)
.body(|mut body| {
round_edit_row("Название", 20.0, &mut body, |ui| {
simple_row("Название", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut round.name);
});
round_edit_row("Вариант", 20.0, &mut body, |ui| {
simple_row("Вариант", 20.0, &mut body, |ui| {
// TODO: variant enum
unselectable_label(format!("TODO: {:?}", round.variant), ui);
});
});
}

fn round_info_edit(info: &mut Option<Info>, ui: &mut egui::Ui) {
let Some(info) = info.as_mut() else {
if ui.button("➕ Добавить информацию").clicked() {
*info = Some(Default::default());
}
return;
};

egui_extras::TableBuilder::new(ui)
.id_salt("round-info-edit")
.column(egui_extras::Column::auto())
.column(egui_extras::Column::remainder())
.cell_layout(egui::Layout::left_to_right(egui::Align::Min))
.striped(true)
.body(|mut body| {
round_edit_row("Авторы", 40.0, &mut body, |ui| {
string_list("round-authors", &mut info.authors, ui);
});
round_edit_row("Источники", 40.0, &mut body, |ui| {
string_list("round-sources", &mut info.sources, ui);
});
round_edit_row("Комментарий", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut info.comments);
});
round_edit_row("Расширения", 20.0, &mut body, |ui| {
ui.text_edit_singleline(&mut info.extension);
});
});
}

fn round_edit_row(
label: impl AsRef<str>,
height: f32,
body: &mut egui_extras::TableBody,
content: impl FnOnce(&mut egui::Ui),
) {
body.row(height, |mut row| {
row.col(|ui| {
ui.label(label.as_ref());
});
row.col(content);
});
}

fn round_themes(
package: &mut Package,
idx: RoundIdx,
selected: &mut Option<PackageNode>,
ui: &mut egui::Ui,
) {
#[derive(Debug, Clone, Copy)]
enum CardKind<'a> {
Theme(&'a Theme),
Question(&'a Question),
New,
}

fn theme_table_card(kind: CardKind, ui: &mut egui::Ui) -> egui::Response {
let (text, fill, text_color) = match kind {
CardKind::Theme(theme) => (
theme.name.clone(),
ui.visuals().widgets.active.bg_fill,
ui.visuals().widgets.active.text_color(),
),
CardKind::Question(question) => (
question.price.to_string(),
egui::Color32::TRANSPARENT,
ui.visuals().widgets.inactive.text_color(),
),
CardKind::New => (
"➕ Новый вопрос".to_string(),
egui::Color32::TRANSPARENT,
ui.visuals().weak_text_color(),
),
};
let mut frame = egui::Frame::default()
.inner_margin(16.0)
.outer_margin(egui::Margin::symmetric(0.0, 4.0))
.stroke(ui.style().visuals.widgets.noninteractive.bg_stroke)
.rounding(8.0)
.fill(fill)
.begin(ui);

// aprox
let font_size = 22.0 - (text.len() as isize - 8).max(0) as f32 * 0.3;
frame.content_ui.add_sized(
egui::vec2(100.0, 0.0),
egui::Label::new(egui::RichText::new(&text).size(font_size).color(text_color))
.selectable(false)
.halign(egui::Align::Center)
.wrap(),
);
let rect =
frame.content_ui.min_rect() + frame.frame.inner_margin + frame.frame.outer_margin;
let response = ui.allocate_rect(rect, egui::Sense::click());
if response.hovered() {
frame.frame.stroke = ui.style().visuals.widgets.active.bg_stroke;
frame.frame.fill = ui.style().visuals.widgets.active.weak_bg_fill;
}
frame.paint(ui);
response.on_hover_text(&text)
}

egui_extras::StripBuilder::new(ui)
.size(egui_extras::Size::remainder())
.size(egui_extras::Size::exact(30.0))
Expand Down Expand Up @@ -198,25 +104,23 @@ fn round_themes(
};

row.col(|ui| {
if theme_table_card(CardKind::Theme(theme), ui).clicked() {
if CardKind::Theme(theme).show(ui).clicked() {
*selected = Some(theme_idx.into());
}
});

for (question_index, question) in theme.questions.iter().enumerate()
{
row.col(|ui| {
if theme_table_card(CardKind::Question(question), ui)
.clicked()
{
if CardKind::Question(question).show(ui).clicked() {
*selected =
Some(theme_idx.question(question_index).into());
}
});
}

row.col(|ui| {
if theme_table_card(CardKind::New, ui).clicked() {
if CardKind::New.show(ui).clicked() {
theme.questions.push(Question {
price: theme.guess_next_question_price(),
..Default::default()
Expand Down
Loading

0 comments on commit ab66b11

Please sign in to comment.