Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plato chess #281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions COPYING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Any file in this project that does not state otherwise and is not listed as an
exception below is part of *plato* and copyright (c) 2017 Bastien Dejean
and plato contributors.

For a list of the authors see the commit log or
https://github.com/baskerville/plato/graphs/contributors/

Plato is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

See the LICENSE file for a copy of the *GNU General Public License*.


Exceptions (free)
-----------------

Files | Author(s) | License
--- | --- | ---
icons/chess|[Colin M.L. Burnett](https://en.wikipedia.org/wiki/User:Cburnett) | [GPLv2+](https://www.gnu.org/licenses/gpl-2.0.txt)
1,374 changes: 1,374 additions & 0 deletions artworks/chess.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ rand_core = "0.6.4"
rand_xoshiro = "0.6.0"
percent-encoding = "2.3.0"
chrono = { version = "0.4.30", features = ["serde", "clock"], default-features = false }
log = "0.4.17"

chess_uci = { git = "https://git.hadoly.fr/bateast/chess_uci.git", optional = true }
chess = { version = "3.2.0", optional = true }
env_logger = { version = "0.10.0", optional = true }


[features]
chess = ["dep:chess_uci", "dep:chess"]
devel = ["dep:env_logger"]

8 changes: 8 additions & 0 deletions crates/core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ fn main() {
"linux" => {
println!("cargo:rustc-link-search=target/mupdf_wrapper/Linux");
println!("cargo:rustc-link-lib=dylib=stdc++");
println!("cargo:rustc-link-lib=dylib=stdc++");
println!("cargo:rustc-link-lib=z");
println!("cargo:rustc-link-lib=bz2");
println!("cargo:rustc-link-lib=jpeg");
println!("cargo:rustc-link-lib=png16");
println!("cargo:rustc-link-lib=gumbo");
println!("cargo:rustc-link-lib=openjp2");
println!("cargo:rustc-link-lib=jbig2dec");
},
"macos" => {
println!("cargo:rustc-link-search=target/mupdf_wrapper/Darwin");
Expand Down
3 changes: 3 additions & 0 deletions crates/core/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
max_width=120
fn_args_layout="Compressed"
short_array_element_width_threshold=25
4 changes: 4 additions & 0 deletions crates/core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub const TEXT_BUMP_LARGE: [u8; 3] = [GRAY11, BLACK, BLACK];
pub const TEXT_INVERTED_SOFT: [u8; 3] = [GRAY05, WHITE, WHITE];
pub const TEXT_INVERTED_HARD: [u8; 3] = [BLACK, WHITE, GRAY06];

pub const CHESSBOARD_BG: u8 = KEYBOARD_BG;
pub const CELL_BLACK: [u8; 2] = [GRAY05, GRAY07];
pub const CELL_WHITE: [u8; 2] = [GRAY10, GRAY07];

pub const SEPARATOR_NORMAL: u8 = GRAY10;
pub const SEPARATOR_STRONG: u8 = GRAY07;

Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/document/mupdf_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use std::mem;

pub const FZ_MAX_COLORS: usize = 32;
#[cfg(target_arch = "x86_64")]
pub const FZ_VERSION: &str = "1.23.3";
#[cfg(target_arch = "arm")]
pub const FZ_VERSION: &str = "1.22.2";

pub const FZ_META_INFO_AUTHOR: &str = "info:Author";
Expand Down
7 changes: 7 additions & 0 deletions crates/core/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ use crate::library::Library;
use crate::font::Fonts;
use crate::app::Context;

#[cfg(feature = "chess")]
use crate::view::plato_chess::PlatoChess;

pub const APP_NAME: &str = "Plato";
const DEFAULT_ROTATION: i8 = 1;

Expand Down Expand Up @@ -413,6 +416,10 @@ fn main() -> Result<(), Error> {
AppCmd::Sketch => {
Box::new(Sketch::new(context.fb.rect(), &mut rq, &mut context))
},
#[cfg(feature = "chess")]
AppCmd::Chess => {
Box::new(PlatoChess::new(context.fb.rect(), &mut rq, &mut context))
},
AppCmd::Calculator => {
Box::new(Calculator::new(context.fb.rect(), &tx, &mut rq, &mut context)?)
},
Expand Down
23 changes: 23 additions & 0 deletions crates/core/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub struct Settings {
pub reader: ReaderSettings,
pub import: ImportSettings,
pub dictionary: DictionarySettings,
#[cfg(feature = "chess")]
pub chess_engine_settings: ChessEngineSettings,
pub sketch: SketchSettings,
pub calculator: CalculatorSettings,
pub battery: BatterySettings,
Expand Down Expand Up @@ -174,6 +176,25 @@ pub struct ImportSettings {
pub allowed_kinds: FxHashSet<String>,
}

#[cfg(feature = "chess")]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct ChessEngineSettings {
pub elo: u32,
pub slow_motion: u32,
pub path: PathBuf,
pub font_size: f32,
pub margin_width: i32,
}

#[cfg(feature = "chess")]
impl Default for ChessEngineSettings {
fn default() -> Self {
ChessEngineSettings { elo: 1500, slow_motion: 100, path: PathBuf::from("bin/stockfish"),
font_size: 8.0, margin_width: 2}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct DictionarySettings {
Expand Down Expand Up @@ -546,6 +567,8 @@ impl Default for Settings {
reader: ReaderSettings::default(),
import: ImportSettings::default(),
dictionary: DictionarySettings::default(),
#[cfg(feature = "chess")]
chess_engine_settings: ChessEngineSettings::default(),
sketch: SketchSettings::default(),
calculator: CalculatorSettings::default(),
battery: BatterySettings::default(),
Expand Down
20 changes: 11 additions & 9 deletions crates/core/src/view/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ pub struct Clock {
}

impl Clock {
pub fn new(rect: &mut Rectangle, context: &mut Context) -> Clock {
let time = Local::now();
let format = context.settings.time_format.clone();
let font = font_from_style(&mut context.fonts, &NORMAL_STYLE, CURRENT_DEVICE.dpi);
let width = font.plan(&time.format(&format).to_string(), None, None).width + font.em() as i32;
rect.min.x = rect.max.x - width;
pub fn new(rect: Rectangle, context: &mut Context) -> Clock {
Clock {
id: ID_FEEDER.next(),
rect: *rect,
rect,
children: Vec::new(),
format,
time,
format: context.settings.time_format.clone(),
time: Local::now(),
}
}

pub fn compute_width(context: &mut Context) -> i32 {
let time = Local::now();
let format = context.settings.time_format.clone();
let font = font_from_style(&mut context.fonts, &NORMAL_STYLE, CURRENT_DEVICE.dpi);
font.plan(&time.format(&format).to_string(), None, None).width + font.em() as i32
}

pub fn update(&mut self, rq: &mut RenderQueue) {
self.time = Local::now();
rq.add(RenderData::new(self.id, self.rect, UpdateMode::Gui));
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/view/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ pub fn toggle_main_menu(view: &mut dyn View, rect: Rectangle, enable: Option<boo
EntryId::Launch(AppCmd::Calculator)),
EntryKind::Command("Sketch".to_string(),
EntryId::Launch(AppCmd::Sketch)),
#[cfg(feature = "chess")]
EntryKind::Command("Chess".to_string(),
EntryId::Launch(AppCmd::Chess)),
EntryKind::Separator,
EntryKind::Command("Touch Events".to_string(),
EntryId::Launch(AppCmd::TouchEvents)),
Expand Down
5 changes: 4 additions & 1 deletion crates/core/src/view/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ lazy_static! {
"close", "check_mark-small", "check_mark", "check_mark-large", "bullet",
"arrow-left", "arrow-right", "angle-down", "angle-up", "crop", "toc", "font_family",
"font_size", "line_height", "align-justify", "align-left", "align-right",
"align-center", "margin", "plug", "cover", "enclosed_menu", "contrast", "gray"].iter().cloned() {
"align-center", "margin", "plug", "cover", "enclosed_menu", "contrast", "gray",].iter()
.chain(if cfg!(feature = "chess"){["wchess", "bchess", "engine-chess", "human-chess",].iter()}
else{[].iter()})
.cloned() {
let path = dir.join(&format!("{}.svg", name));
let doc = PdfOpener::new().and_then(|o| o.open(path)).unwrap();
let pixmap = doc.page(0).and_then(|p| p.pixmap(scale)).unwrap();
Expand Down
9 changes: 8 additions & 1 deletion crates/core/src/view/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Label {
align: Align,
event: Option<Event>,
hold_event: Option<Event>,
font_size: u32,
}

impl Label {
Expand All @@ -27,6 +28,7 @@ impl Label {
align,
event: None,
hold_event: None,
font_size: NORMAL_STYLE.size,
}
}

Expand All @@ -46,6 +48,10 @@ impl Label {
rq.add(RenderData::new(self.id, self.rect, UpdateMode::Gui));
}
}

pub fn set_font_size(&mut self, font_size: f32) {
self.font_size = (64.0 * font_size) as u32;
}
}

impl View for Label {
Expand All @@ -72,7 +78,8 @@ impl View for Label {

fb.draw_rectangle(&self.rect, TEXT_NORMAL[0]);

let font = font_from_style(fonts, &NORMAL_STYLE, dpi);
let mut style = NORMAL_STYLE; style.size = self.font_size;
let font = font_from_style(fonts, &style, dpi);
let x_height = font.x_heights.0 as i32;
let padding = font.em() as i32;
let max_width = self.rect.width() as i32 - padding;
Expand Down
58 changes: 58 additions & 0 deletions crates/core/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ pub mod calculator;
pub mod sketch;
pub mod touch_events;
pub mod rotation_values;
#[cfg(feature = "chess")]
pub mod plato_chess;

#[cfg(feature = "devel")]
use {
crate::device::CURRENT_DEVICE,
};

use std::ops::{Deref, DerefMut};
use std::time::{Instant, Duration};
Expand All @@ -63,6 +70,12 @@ use crate::gesture::GestureEvent;
use self::calculator::LineOrigin;
use self::key::KeyKind;
use crate::context::Context;
#[cfg(feature = "chess")]
use {
chess::Square,
chess_uci::uci_command::EngineCommand,
};
use log::{debug, warn};

// Border thicknesses in pixels, at 300 DPI.
pub const THICKNESS_SMALL: f32 = 1.0;
Expand Down Expand Up @@ -294,6 +307,12 @@ pub enum Event {
Gesture(GestureEvent),
Keyboard(KeyboardEvent),
Key(KeyKind),
#[cfg(feature = "chess")]
ChessCommand(EngineCommand),
#[cfg(feature = "chess")]
ChessGo(bool),
#[cfg(feature = "chess")]
ChessCell(Square, bool),
Open(Box<Info>),
OpenHtml(String, Option<String>),
LoadPixmap(usize),
Expand Down Expand Up @@ -350,6 +369,12 @@ pub enum Event {
Scroll(i32),
Save,
Guess,
#[cfg(feature = "chess")]
Reset,
#[cfg(feature = "chess")]
UpdateValue(f32),
#[cfg(feature = "chess")]
SaveAll(ViewId, Vec<f32>),
CheckBattery,
SetWifi(bool),
MightSuspend,
Expand All @@ -368,6 +393,8 @@ pub enum Event {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum AppCmd {
Sketch,
#[cfg(feature = "chess")]
Chess,
Calculator,
Dictionary {
query: String,
Expand Down Expand Up @@ -408,6 +435,14 @@ pub enum ViewId {
MarginCropperMenu,
SearchMenu,
SketchMenu,
#[cfg(feature = "chess")]
ChessMenu,
#[cfg(feature = "chess")]
ChessSettings,
#[cfg(feature = "chess")]
ChessPromotion,
#[cfg(feature = "chess")]
ChessMoves,
RenameDocument,
RenameDocumentInput,
GoToPage,
Expand Down Expand Up @@ -444,6 +479,10 @@ pub enum SliderId {
LightWarmth,
ContrastExponent,
ContrastGray,
#[cfg(feature = "chess")]
ChessElo,
#[cfg(feature = "chess")]
ChessSlow,
}

impl SliderId {
Expand All @@ -454,6 +493,10 @@ impl SliderId {
SliderId::FontSize => "Font Size".to_string(),
SliderId::ContrastExponent => "Contrast Exponent".to_string(),
SliderId::ContrastGray => "Contrast Gray".to_string(),
#[cfg(feature = "chess")]
SliderId::ChessElo => "Elo level calibration".to_string(),
#[cfg(feature = "chess")]
SliderId::ChessSlow => "Slow motion".to_string(),
}
}
}
Expand Down Expand Up @@ -521,6 +564,14 @@ pub enum EntryId {
CopyTo(PathBuf, usize),
MoveTo(PathBuf, usize),
AddDirectory(PathBuf),
#[cfg(feature = "chess")]
ChessPlayer(chess::Color, chess_uci::Player),
#[cfg(feature = "chess")]
ChessPiece(chess::ChessMove),
#[cfg(feature = "chess")]
ChessEngineSettings,
#[cfg(feature = "chess")]
ChessTimeControl(u64, u64),
SelectDirectory(PathBuf),
ToggleSelectDirectory(PathBuf),
SetStatus(PathBuf, SimpleStatus),
Expand Down Expand Up @@ -612,6 +663,7 @@ impl EntryKind {
}
}

#[derive(Debug)]
pub struct RenderData {
pub id: Option<Id>,
pub rect: Rectangle,
Expand Down Expand Up @@ -671,6 +723,12 @@ impl RenderQueue {
}

pub fn add(&mut self, data: RenderData) {
debug!("adding to render queue {:?}", data);
#[cfg(feature = "devel")]
if (data.rect.max.x as u32, data.rect.max.y as u32) > CURRENT_DEVICE.dims {
warn!("rect ({:?})out of screen limits {:?}", data.rect, CURRENT_DEVICE.dims);
}

self.entry((data.mode, data.wait)).or_insert_with(|| {
Vec::new()
}).push((data.id, data.rect));
Expand Down
Loading