clean up single-view version of cocoatea

This commit is contained in:
Paul Davis 2024-11-19 12:11:02 -07:00
parent 56a2c9978c
commit bfffe101de
3 changed files with 69 additions and 0 deletions

23
cocoatea/meter.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include <AppKit/AppKit.h>
#include "view.h"
struct Meter {
double x;
double y;
double width;
double height;
double r;
double g;
double b;
double a;
double level;
NSView* view;
Meter (NSView* view, double ax, double ay, double aw, double ah, double ar, double ag, double ab, double aa);
void set_level (double);
void draw (CGContextRef);
};

44
cocoatea/meter.mm Normal file
View File

@ -0,0 +1,44 @@
/* -*- c++ -*- */
#include "meter.h"
#include "view.h"
Meter::Meter (NSView* v, double ax, double ay, double aw, double ah, double ar, double ag, double ab, double aa)
: x (ax)
, y (ay)
, width (aw)
, height (ah)
, r (ar)
, g (ag)
, b (ab)
, a (aa)
, level (0.)
, view (v)
{
}
void
Meter::draw (CGContextRef cg)
{
NSRect bbox = NSMakeRect (x, y, width, height);
if (! [view needsToDrawRect:bbox]) {
return;
}
CGContextSetRGBStrokeColor (cg, 1., 1., 1., 1.);
CGContextStrokeRect (cg, NSMakeRect (x, y, width, height));
CGContextSetRGBFillColor (cg, r, g, b, a);
double fill_height = height - (height * level);
CGContextFillRect (cg, NSMakeRect (x, y + fill_height, width, height - fill_height));
CGContextSetRGBFillColor (cg, 0, 0., 0., 1.0);
CGContextFillRect (cg, NSMakeRect (x, y, width, fill_height));
}
void
Meter::set_level (double f)
{
level = f;
[view setNeedsDisplayInRect:NSMakeRect (x, y, width, height)];
}

View File

@ -1,3 +1,5 @@
#pragma once
#include <AppKit/AppKit.h>
@interface CTView : NSView