Lazarus

Programming => LCL => Topic started by: MarkMLl on June 19, 2021, 01:01:27 pm

Title: "MS-Paint" clone
Post by: MarkMLl on June 19, 2021, 01:01:27 pm
Preferably using standard components, is there an easy way to have an "intentionally crude" bitmap editor with visible grid like the original MS-Paint?

I want to write something quick-and-dirty to test some other software, which has a bitmap editor of say 150 cols x 64 rows and some per-row configuration stuff. I'm not sure whether a TDrawGrid can be drawn on using the mouse etc., and a TPaintBox looks far too "finessed".

Any suggestions would be appreciated, I rarely do anything which is graphical.

MarkMLl
Title: Re: "MS-Paint" clone
Post by: Fred vS on June 19, 2021, 01:30:01 pm
This. (https://github.com/bgrabitmap/lazpaint/releases/tag/v7.1.6)

Fre;D
Title: Re: "MS-Paint" clone
Post by: MarkMLl on June 19, 2021, 02:23:06 pm
Thanks, I'll take a look but its excessive resolution and lack of explicit gridding suggests that I might be better off with a TDrawGrid... even if I'm not able to draw lines etc. directly onto it.

MarkMLl
Title: Re: "MS-Paint" clone
Post by: MarkMLl on June 19, 2021, 05:29:31 pm
Thanks for that Jamie. I must admit that I've yet to get to grips with some of the inbuilt grid editing stuff, but this is a test program and being able to get at each row's parameters is more important than interactive drawing.

MarkMLl
Title: Re: "MS-Paint" clone
Post by: wp on June 19, 2021, 06:05:00 pm
Here is a little (incomplete) demo to show the principle. The basic idea is that there must be a bitmap in which your draw by using the bitmap's canvas methods. After each drawing step you repaint the drawgrid which reads the bitmap pixel by pixel and paints the cells corresponding to the pixel colors.
Title: Re: "MS-Paint" clone
Post by: MarkMLl on June 19, 2021, 07:52:12 pm
Jamie, Wp: thanks. In practice each cell represents a single pixel, and I'm exercising various encoding/decoding stuff where a different algorithm and optional palette may be selected for each row.

I've got something roughed out which is adequate, and am moving on to the various dialog(u)es/forms before getting back into the low-level code.

MarkMLl
Title: Re: "MS-Paint" clone
Post by: MarkMLl on June 20, 2021, 09:21:42 am
I wonder whether somebody who knows what he's doing could spare a moment to look at the attached and tell me what I'm doing wrong.

I've hung an event onto the TDrawGrid to invoke a custom editor form or dialogue, it works as expected the first time one of the left-hand cells is clicked but after that it's triggered by a mouseover. Lazarus trunk (as of a few days ago), both gtk2 and qt5.

MarkMLl
Title: Re: "MS-Paint" clone
Post by: wp on June 20, 2021, 10:57:37 am
Your example cannot be compiled because it is missing the SelectRowFormatCode unit. Luckily it is not needed, after removing it from uses, I could see the issue.

I think that OnSelectCell is a very complex event. I would not use it to show something interruptive like a message box. The problem is that the internal state variable FGridState was set in the OnMouseDown event to gsSelecting and is still like that after the message box was clicked away. So, the grid is still in selecting state which fires the OnSelectCell again when your mouse enters one of these cells a second time. The FGridState would be reset in the MouseUp event but this never occurs because of the ShowMessage.

A better solution would be to use the OnClick event to display the ShowMessage , and to use OnSelectCell only to control whether a cell can be selected or not (CanSelect parameter). Of course you need some status variables to control the appearance of the message box in the general purpose OnClick event.
Title: Re: "MS-Paint" clone
Post by: MarkMLl on June 20, 2021, 11:28:35 am
Your example cannot be compiled because it is missing the SelectRowFormatCode unit. Luckily it is not needed, after removing it from uses, I could see the issue.

Sorry, I removed the modal window in case that was related and /thought/ I'd commented out the unit.

Quote
I think that OnSelectCell is a very complex event. I would not use it to show something interruptive like a message box. The problem is that the internal state variable FGridState was set in the OnMouseDown event to gsSelecting and is still like that after the message box was clicked away. So, the grid is still in selecting state which fires the OnSelectCell again when your mouse enters one of these cells a second time. The FGridState would be reset in the MouseUp event but this never occurs because of the ShowMessage.

A recent comment by Remy also alluded to the component complexity. So basically, there are strict limits on what can be put in OnSelectCell etc. and it's down to me to familiarise myself with the "proper practice".

I suppose this would also explain why some event handlers can be tricky to debug... I fairly regularly find myself locking up KDE and needing to kill the debuggee from a text session <Ctrl><Alt><F1>.

Quote
A better solution would be to use the OnClick event to display the ShowMessage , and to use OnSelectCell only to control whether a cell can be selected or not (CanSelect parameter). Of course you need some status variables to control the appearance of the message box in the general purpose OnClick event.

The messagebox was basically a placeholder after I ripped out the modal window. The convenience of OnSelectCell is that it passes the column and row as parameters... could somebody remind me of the best alternative where a handler e.g. OnClick doesn't provide these?

MarkMLl
Title: Re: "MS-Paint" clone
Post by: wp on June 20, 2021, 11:31:49 am
The convenience of OnSelectCell is that it passes the column and row as parameters... could somebody remind me of the best alternative where a handler e.g. OnClick doesn't provide these?
The position of the active cell can be retrieved from the .Col and .Row properties of the grid.
Title: Re: "MS-Paint" clone
Post by: MarkMLl on June 20, 2021, 11:39:19 am
The convenience of OnSelectCell is that it passes the column and row as parameters... could somebody remind me of the best alternative where a handler e.g. OnClick doesn't provide these?
The position of the active cell can be retrieved from the .Col and .Row properties of the grid.

Many thanks for that, looking good. I was being extremely wary lest a click was processed distinct from the action of a cell being selected or set active.

MarkMLl
TinyPortal © 2005-2018