Compare commits
3 commits
ad360ed96e
...
ae42cd10a4
Author | SHA1 | Date | |
---|---|---|---|
ae42cd10a4 | |||
cd51c4b47a | |||
e2df4f4bff |
4 changed files with 88 additions and 14 deletions
|
@ -5,7 +5,14 @@
|
||||||
"id": "output",
|
"id": "output",
|
||||||
"name": "Zero",
|
"name": "Zero",
|
||||||
"description": "learn how to output data",
|
"description": "learn how to output data",
|
||||||
"init_board": "\n o \n\n I\n\n",
|
"init_board": {
|
||||||
|
"comments": [
|
||||||
|
{ "text": "Welcome :3", "x": 3, "y": 0 },
|
||||||
|
{ "text": "< This is a marble,\n it will move down when you start the machine", "x": 3, "y": 2 },
|
||||||
|
{ "text": "< This is an input/output silo\n when a marble enters it, it disappears\n and the value it held is added to the level output", "x": 3, "y": 5 }
|
||||||
|
],
|
||||||
|
"grid": "\n\n o \n\n\n I\n\n\n"
|
||||||
|
},
|
||||||
"stages": [{
|
"stages": [{
|
||||||
"input": [],
|
"input": [],
|
||||||
"output": [0]
|
"output": [0]
|
||||||
|
|
81
src/board.rs
81
src/board.rs
|
@ -1,15 +1,24 @@
|
||||||
|
use raylib::{
|
||||||
|
color::Color,
|
||||||
|
drawing::{RaylibDraw, RaylibDrawHandle},
|
||||||
|
math::Vector2,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::marble_engine::{
|
use crate::{
|
||||||
grid::{Grid, ResizeDeltas},
|
marble_engine::{
|
||||||
pos::Pos,
|
grid::{Grid, ResizeDeltas},
|
||||||
tile::Tile,
|
pos::Pos,
|
||||||
|
tile::Tile,
|
||||||
|
},
|
||||||
|
theme::TILE_TEXTURE_SIZE,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
text: String,
|
text: String,
|
||||||
pos: Pos,
|
x: i32,
|
||||||
|
y: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
@ -62,9 +71,33 @@ impl Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn draw_comments(&self, d: &mut RaylibDrawHandle, offset: Vector2, scale: f32) {
|
||||||
|
let tile_size = (TILE_TEXTURE_SIZE * scale) as i32;
|
||||||
|
let font_size = 10 * (scale as i32).max(1);
|
||||||
|
let line_space = 12 * (scale as i32).max(1);
|
||||||
|
|
||||||
|
for comment in &self.comments {
|
||||||
|
let x = comment.x * tile_size + offset.x as i32;
|
||||||
|
let y = comment.y * tile_size + offset.y as i32;
|
||||||
|
for (i, line) in comment.text.lines().enumerate() {
|
||||||
|
let y = y + line_space * i as i32;
|
||||||
|
d.draw_text(&line, x, y, font_size, Color::ORANGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_rect(&self, pos: Pos, width: usize, height: usize) -> Self {
|
pub fn get_rect(&self, pos: Pos, width: usize, height: usize) -> Self {
|
||||||
// TODO filter for comments in the area
|
let comments = self
|
||||||
let comments = self.comments.clone();
|
.comments
|
||||||
|
.iter()
|
||||||
|
.filter(|c| c.in_area(pos, width, height))
|
||||||
|
.map(|c| {
|
||||||
|
let mut c = c.clone();
|
||||||
|
c.x -= pos.x as i32;
|
||||||
|
c.y -= pos.y as i32;
|
||||||
|
c
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
Self {
|
Self {
|
||||||
grid: self.grid.get_rect(pos, width, height),
|
grid: self.grid.get_rect(pos, width, height),
|
||||||
comments,
|
comments,
|
||||||
|
@ -72,19 +105,36 @@ impl Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn paste_board(&mut self, pos: Pos, board: &Board) {
|
pub fn paste_board(&mut self, pos: Pos, board: &Board) {
|
||||||
// TODO remove overlapping comments
|
|
||||||
self.grid.paste_grid(pos, &board.grid);
|
self.grid.paste_grid(pos, &board.grid);
|
||||||
self.comments.extend_from_slice(&board.comments);
|
for c in &board.comments {
|
||||||
|
let mut comment = c.clone();
|
||||||
|
comment.x += pos.x as i32;
|
||||||
|
comment.y += pos.y as i32;
|
||||||
|
self.add_comment(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_comment(&mut self, comment: Comment) {
|
||||||
|
if self.comments.iter().any(|c| c == &comment) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.comments.push(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grow(&mut self, deltas: &ResizeDeltas) {
|
pub fn grow(&mut self, deltas: &ResizeDeltas) {
|
||||||
self.grid.grow(deltas);
|
self.grid.grow(deltas);
|
||||||
// TODO adjust comments
|
for c in &mut self.comments {
|
||||||
|
c.x += deltas.x_neg as i32;
|
||||||
|
c.y += deltas.y_neg as i32;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shrink(&mut self, deltas: &ResizeDeltas) {
|
pub fn shrink(&mut self, deltas: &ResizeDeltas) {
|
||||||
self.grid.shrink(deltas);
|
self.grid.shrink(deltas);
|
||||||
// TODO adjust comments
|
for c in &mut self.comments {
|
||||||
|
c.x -= deltas.x_neg as i32;
|
||||||
|
c.y -= deltas.y_neg as i32;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_user_str(source: &str) -> Self {
|
pub fn from_user_str(source: &str) -> Self {
|
||||||
|
@ -102,3 +152,12 @@ impl Board {
|
||||||
self.grid.height()
|
self.grid.height()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Comment {
|
||||||
|
fn in_area(&self, pos: Pos, width: usize, height: usize) -> bool {
|
||||||
|
self.x >= pos.x as i32
|
||||||
|
&& self.y >= pos.y as i32
|
||||||
|
&& self.x < pos.x as i32 + width as i32
|
||||||
|
&& self.y < pos.y as i32 + height as i32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ impl Editor {
|
||||||
machine,
|
machine,
|
||||||
sim_state: SimState::Editing,
|
sim_state: SimState::Editing,
|
||||||
view_offset: Vector2::zero(),
|
view_offset: Vector2::zero(),
|
||||||
zoom: 1.,
|
zoom: 2.,
|
||||||
active_tool: Tool::None,
|
active_tool: Tool::None,
|
||||||
output_as_text,
|
output_as_text,
|
||||||
input_as_text,
|
input_as_text,
|
||||||
|
@ -553,7 +553,7 @@ impl Editor {
|
||||||
self.pasting_board = Some(b);
|
self.pasting_board = Some(b);
|
||||||
}
|
}
|
||||||
} else if rl.is_key_pressed(KeyboardKey::KEY_Z) {
|
} else if rl.is_key_pressed(KeyboardKey::KEY_Z) {
|
||||||
self.undo()
|
self.undo();
|
||||||
} else if rl.is_key_pressed(KeyboardKey::KEY_Y) {
|
} else if rl.is_key_pressed(KeyboardKey::KEY_Y) {
|
||||||
self.redo();
|
self.redo();
|
||||||
}
|
}
|
||||||
|
@ -584,6 +584,10 @@ impl Editor {
|
||||||
.draw_marble_values(d, textures, self.view_offset, self.zoom);
|
.draw_marble_values(d, textures, self.view_offset, self.zoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if self.draw_overlay {
|
||||||
|
self.source_board
|
||||||
|
.draw_comments(d, self.view_offset, self.zoom);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
|
pub fn draw(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
|
||||||
|
|
|
@ -529,6 +529,10 @@ impl Machine {
|
||||||
}
|
}
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
|
self.debug_subticks.push(DebugSubTick {
|
||||||
|
grid: self.grid.clone(),
|
||||||
|
pos: None,
|
||||||
|
});
|
||||||
self.subtick_index = self.debug_subticks.len() - 1;
|
self.subtick_index = self.debug_subticks.len() - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue