Hex-Draw: Getting to know the hexagonal grid

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

Hex-draw is a program for drawing on a hexagonal (not-cartesian) grid.
The program uses the keyboard for all input and does not use the mouse.
Since it uses all the unique keys of the keyboard,
I have used GetRawKeyCode instead of GetKeyCode
and gotten it to work for my specific keyboard's raw key codes.
You will have to change this to get it to work on your system.

Some UI tips:

The figure below gives an approximate idea of the controls
http://hex-map.khitchdee.net/hex-draw.html
The escape key exits the app.
The 3 keys on the left edge and right edge serve for moving the crosshair (~mouse pointer).
The space-bar serves as an execute key.
The typewriter keys which you see onscreen when the app is launched
can also be used to move the cross-hair (which is like the mouse) to the mapped location on the screen.
The backspace key can be used to move the keyboard-map to the crosshair.
press some combination of the arrow keys to setup the crosshair for movement on the grid
then use the spacebar to execute the movement

There is a small amount of wx specific code at the top.
Beyond that, it's all a set of C function calls.
The best way to read the code is first the wx boilerplate at the top
and then from the bottom up to see what the app is doing.
If your editor shows a function list (e.g. Xcode does), use that to navigate
from the bottom to the top.
A bunch of #defines have been added to demarcate the functions in this function list view.

It's very rough at the edges right now, but will get better with time.

The code is all in one file hex-draw.cpp attached
Attachments
hex-draw.cpp
(80.65 KiB) Downloaded 60 times
Last edited by Rohit Agarwal on Sun Aug 08, 2021 5:30 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 16399
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Hex-Draw: Getting to know the hexagonal grid

Post by doublemax »

I think your drawing code needs some refactoring. Not only do you leak all wxPaintDCs, the fact you're creating multiple of them during a single redraw is at least sub-optimal. I don't know if this is supposed to work or not, but as there is no need to do so, i'd get rid of it.

What i'd do:
Turn all the drawing functions that take a MyCanvas as parameter into methods of MyCanvas, so you don't have to pass the MyCanvas* pointer around all the time.

But you should create one single wxPaintDC in MyCanvas::OnPaint() and pass it to lower methods, so they can use it to draw onto.

Also, instead of passing a wxPaintDC&, just use wxDC& as parameter, in case you want to draw onto another type of wxDC in the future.

Also, you should (re-)draw everything in a paint event handler. Sometimes you're only redrawing parts, and as you're drawing with transparency, this will lead to redraw errors. (Parts that get darker on each redraw).

I tested under Windows, it is possible that your code works fine under OSX.
Use the source, Luke!
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

Only one PaintDC is created for each redraw.
The app tracks where the redraw came from and sends that function a request to do the draw.
The function knows exactly what it needs to redraw since it sent the refresh itself.
I didn't know you're supposed to delete DCs, since I assumed wx does it.
Is this not the case?

Once you get the code running you'll understand better how it's architected.
I am on OSX BigSur 11.1
User avatar
doublemax
Moderator
Moderator
Posts: 16399
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Hex-Draw: Getting to know the hexagonal grid

Post by doublemax »

I didn't know you're supposed to delete DCs, since I assumed wx does it.
Is this not the case?
No. Usually they are created on the stack.
Use the source, Luke!
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

The code does not need any other libraries than base and core.
So, to build under windows, you only need to link with those 2
and whatever libraries those 2 need to get their job done.
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

I built in on a PC
There are several warning in the build process
and I got a segmentation fault when I ran it.
It runs fine on my MAC which is my primary dev platform.
I will fix this and repost with more updates.
It's derived from the drawing sample.
Simplest way to build it
is replace drawing.cpp in the base samples folder with hex-draw.cpp (renamed to drawing.cpp)
and build the drawing sample.

I've attached a screenshot from my MAC:
Attachments
Screenshot.png
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

The code now builds and runs on windows and on GTK.
But the key maps are all broken so you can only see the grid for now.
They are currently hard-coded for an ANSI keyboard on a MAC
since they use raw key codes rather than key codes.
I'll post a keyboard calibration update that will take care of that next.

EDIT: Updated the code to create DC on stack. Key-map inline with conventions
Runs fine on Mac(M1), some errors on Windows and GTK.
Key-map based navigation works. Escape to exit.

This is the website for hex-draw:
https://hex-map.khitchdee.net/hex-draw.html
Attachments
hex-draw.cpp
(81.33 KiB) Downloaded 41 times
Last edited by Rohit Agarwal on Wed Aug 25, 2021 9:24 am, edited 1 time in total.
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

Here's a calibration update.
It builds and runs on Mac and Windows with the same build settings as the drawing sample.
When you run it, it takes you through a sequence of steps
to calibrate your system for use with hex-draw.
This calibration data is saved in a file called cal.dat.
The second time you run the app, it loads your calibration data from this file.

Functionally, it demonstrates on-screen space selection
via an on-screen hexagonal grid
and a direct-map of the keyboard's keys to points on the grid.
The ability to move this map enables selection of any grid-point on the screen.

This is a demonstration of the potential for building UIs on a hex-grid.

Note: to re-calibrate your system, just delete cal.dat in the app's directory

https://hex-map.khitchdee.net/getting-started.html
Attachments
kybd-cal.cpp
(115.8 KiB) Downloaded 28 times
Last edited by Rohit Agarwal on Wed Aug 25, 2021 9:25 am, edited 2 times in total.
Rohit Agarwal
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: Hex-Draw: Getting to know the hexagonal grid

Post by Rohit Agarwal »

Here's a screenshot of a bike sketched using hex-draw.
It took about an hour to sketch.
It illustrates the feasibility of efficient screen-space selection
using a hex-mapped keyboard and a hexagonal grid without using the mouse.
Screenshot 2021-08-10 at 7.07.32 PM.png
Here's the Inkscape drawing from which it was mapped.
bike.png
bike.png (16.39 KiB) Viewed 2137 times
EDIT:

The code that was used to sketch the bike.
Drawing controls are explained briefly.
They are a bit clumsy.
It builds and runs on Mac, Windows and Ubuntu.
Please ignore the compiler warnings.
It's about 4000 lines of mostly C code

If you have the older code running, delete cal.dat before running this code.
Attachments
kybd-cal.cpp
(115.56 KiB) Downloaded 21 times
Post Reply