Page 1 of 1

SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 5:54 am
by netmaestro
Hello,

I made a try at a MacOS program. I'm not very familiar with the OSX environment yet so please be patient with my noobish approach to everything. (in particular I didn't spend a lot of time on the rotation angles so if the thing suddenly goes berserk or takes on a mind of its own don't be too shocked :mrgreen: )

You will need this image: http://lloydsplace.com/clockface-blue.png

And then make it available to this code:

Code: Select all

;///////////////////////////////////////////////////////////////
; Program:            SimpleClock
; Author:             Lloyd Gallant (netmaestro)
; Date:               February 28, 2016
; Target OS:          Mac OSX (not sure the version req'd)
; Compiler Used:      PureBasic 5.42 LTS Beta 4
; License:            Unlimited, do as you like with it
;///////////////////////////////////////////////////////////////
;
;===================================
;           DECLARATIONS
;===================================
Declare RightClickHandler()
Declare MenuHandler()
Declare InitializeEvents()
Declare InitializeWindow()
Declare UpdateClock()

;===================================
;            CONSTANTS
;===================================
Enumeration
  #Output
  #Face
  #Hour
  #Minute
  #Second
EndEnumeration

;===================================
;             GLOBALS
;===================================
Global UserRequest_Quit=0

;===================================
;            RESOURCES
;===================================
DataSection
  clockface:
  IncludeBinary "clockface-blue.png"
EndDataSection

UsePNGImageDecoder()
CatchImage(#Face, ?clockface)

CreateImage(#Hour, 128, 128, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(#Hour))
  MovePathCursor(63,73)
  VectorSourceColor(RGBA(230, 230, 230, 255))
  AddPathLine(63, 42)
  StrokePath(5, #PB_Path_RoundEnd)
  FillPath()
StopVectorDrawing()

CreateImage(#Minute, 128, 128, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(#Minute))
  MovePathCursor(63,73)
  VectorSourceColor(RGBA(180, 180, 180, 255))
  AddPathLine(63, 25)
  StrokePath(5, #PB_Path_RoundEnd)
  FillPath()
StopVectorDrawing()

CreateImage(#Second, 128, 128, 32, #PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(#Second))
  MovePathCursor(63,73)
  VectorSourceColor(RGBA(255, 0, 0, 255))
  AddPathLine(63, 20)
  StrokePath(1.5, #PB_Path_RoundEnd)
  FillPath()
StopVectorDrawing()

;===================================
;           MAIN LOOP
;===================================
InitializeWindow()
Repeat : EventID = WaitWindowEvent() : Until UserRequest_Quit

;===================================
;        CALLED PROCEDURES
;===================================
Procedure UpdateClock()
  Static LastSecond.i, ms.i, Delta.i
  Protected Hour.i, Minute.i, Second.i, Angle_Hour.d, Angle_Minute.d, Angle_Second.d
  Hour = Hour(Date()):If Hour>12:Hour-12:EndIf
  Minute = Minute(Date())
  Second = Second(Date())
  If Second<>LastSecond
    LastSecond=Second
    ms=ElapsedMilliseconds()
    Delta=0
  Else
    delta=ElapsedMilliseconds()-ms
  EndIf
  Angle_Minute = (Minute / 60) * 360
  Angle_Hour = (Hour / 12) * 360 + (Angle_Minute/360)*(360/12)
  Angle_Second = ((Second+(Delta/1000)) / 60) * 360
  CopyImage(#Face, #Output)
  StartVectorDrawing(ImageVectorOutput(#Output))
    ResetCoordinates()
    RotateCoordinates(63,63,Angle_Hour)
    MovePathCursor(0,0)
    DrawVectorImage(ImageID(#Hour))    
    ResetCoordinates()
    RotateCoordinates(63,63,Angle_Minute)
    MovePathCursor(0,0)
    DrawVectorImage(ImageID(#Minute))       
    ResetCoordinates()
    RotateCoordinates(63,63,Angle_Second)
    MovePathCursor(0,0)
    DrawVectorImage(ImageID(#Second))       
  StopVectorDrawing()
  CocoaMessage(0, WindowID(0), "setBackgroundColor:", CocoaMessage(0, 0, "NSColor colorWithPatternImage:", ImageID(#Output)))
EndProcedure

Procedure InitializeEvents()
  BindEvent(#PB_Event_Timer, @UpdateClock())
  BindEvent(#PB_Event_RightClick, @RightClickHandler())
  BindEvent(#PB_Event_Menu, @MenuHandler())
EndProcedure

Procedure InitializeWindow()
  OpenWindow(0, 0, 0, 128, 128, "", #PB_Window_BorderLess|#PB_Window_ScreenCentered|#PB_Window_Invisible)
  CocoaMessage(0, WindowID(0), "setOpaque:", #NO)
  CocoaMessage(0, WindowID(0), "setMovableByWindowBackground:", #YES)
  CocoaMessage(0, WindowID(0), "setHasShadow:", #YES)
  UpdateClock()
  CreatePopupMenu(0)
  MenuItem(9, "Close the clock")
  MenuItem(10, "Cancel")
  InitializeEvents()
  HideWindow(0, 0)
  AddWindowTimer(0, 1, 50)
EndProcedure

Procedure RightClickHandler()
  DisplayPopupMenu(0, WindowID(0))
EndProcedure

Procedure MenuHandler()
  Select EventMenu()
    Case 9
      UserRequest_Quit = #True
  EndSelect
EndProcedure
As always, critique and suggestions are welcome, I hope you have some fun with it!

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 6:53 am
by wilbert
Beautiful :)
Very good for a first try !

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 7:44 am
by netmaestro
Thanks wilbert! Making this I was struck by how much can be accomplished with a small amount of code (and effort) with the modern version of PureBasic in the OSX environment. I made something similar to this a few years back on the Windows platform and it required far more API programming to achieve the same result. With the arrival of BindEvent() threads can be dispensed with, the vector drawing lib makes calling OS libraries unnecessary for things like antialias and rotations, and using custom-shaped images for a window canvas is just a couple of calls to Cocoa. Gdiplus on Windows is such a pain with all the prototypes/imports required and it's so nice to just leave that all behind and use native PureBasic for the heavy lifting. Fred and Freak are real-life heroes for the work they've done on PB and the selfless way they've made it available so inexpensively. The gift that keeps on giving!

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 8:53 am
by Keya
congrats on taking the plunge with OSX! its both liberating and empowering not to be stuck in one OS and being able to offer your software to more people huh!? i wouldnt have taken that step without Purebasic either :) special thanks also to wilbert for all his work behind the scenes that allowed Fred to implement CocoaMessage!!

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 6:12 pm
by WilliamL
Very smooth! ..and and example of using vector graphics. :D

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 6:21 pm
by Fred
Now, I just booted my mac just to see the newest netmeastro creation ! :twisted:

Nice, as always

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 9:55 pm
by davido
Excellent, as usual.
Thank you for sharing.

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 10:01 pm
by normeus
Currently using xcode for Apple Dev. Thanks to netmaestro's example; I will give PureBasic OSX a try!

Thank you.
Norm.

Re: SimpleClock: My first try with MacOS

Posted: Mon Feb 29, 2016 11:39 pm
by Andre
Very nice example! :D

Re: SimpleClock: My first try with MacOS

Posted: Tue Mar 01, 2016 7:13 am
by Keya
normeus wrote:Currently using xcode for Apple Dev. Thanks to netmaestro's example; I will give PureBasic OSX a try!
very surprised to see this! I wouldve assumed everyone here that has OSX would already be using Purebasic OSX, but you haven't even tried it yet!? :)

I'm new to OSX and Linux myself, I only tried them because Purebasic itself supports them, but Purebasic for OSX and Linux is very much the same so the learning curve is very small! actually I found most of the learning curve to be in the OS itself, not PB, so if you know the OS well already what are you waiting for!? It's great how most code runs with very few modifications on OSX and Linux, rarely having to use CompilerIf #PB_Compiler_OS statements but able to get the job done perfectly with them when required. Happy days! :)

Re: SimpleClock: My first try with MacOS

Posted: Tue Mar 01, 2016 9:35 am
by TI-994A
Never thought that I'd be impressed by a clock demo. Simply lovely! :lol:

Re: SimpleClock: My first try with MacOS

Posted: Mon Mar 14, 2016 8:31 pm
by fsw
Keya wrote:
normeus wrote:Currently using xcode for Apple Dev. Thanks to netmaestro's example; I will give PureBasic OSX a try!
very surprised to see this! I wouldve assumed everyone here that has OSX would already be using Purebasic OSX, but you haven't even tried it yet!? :)
... so if you know the OS well already what are you waiting for!?
One example: waiting for drag&drop functionality to work well on OS X...