 |
 |
help, Solitaire is kicking my arse!
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
Ok, I'm not sure quite how to word this, so I'm just gonna explain my situation. I'm a beginning CS major, and for my Data Structures class(think 2nd semester C++), we were assigned a project with a couple of options, one of which was to program a program in a different language. Being the glutton for punishment that I am, I chose to write a solitaire application in obj-C, using cocoa. I've been working on it, learned a lot, but I've worked myself into a corner.
I've developed all my classes that i need (I think), and I'm ready to set up the GUI, but I have NO clue how to go about it. I'm one of those people who learns better by example, But I can't find anything in the way of example code to hold my hand through this. so, I turn to you, the wise guru's of the Dev Center, hoping someone will give me some hints as to how to deal with dragging cards, and especially stacks of cards where I may need to deal with the cards underneath.
Here's what I have done so far, so y'all know I'm not just trying to get someone to do my homework!!
http://www.boomspeed.com/cheerios18/
Thanks, in advance, for any advice or help you guys may be able to offer!!
|
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
Well, judging from the data structures you have so far, it looks like what you'll want to do in order to display the cards is to create an NSView subclass that will draw a single stack of cards. Then you'd create a window with a bunch of instances of this view as your playing table. I'm assuming this is the standard Klondike-type solitare game, so you'd have 4 views for the win stacks, 7 views for the play stacks, a win stack, etc. This view might also have to support slightly different ways of displaying, as the stacks might want to appear differently. For instance, the 7 play stacks will want to be displayed from the top down, the win stacks probably only the top card, etc. You could make subclasses for the various different types of stacks that would draw differently, but have some support methods in the common view class for things like drawing a single card, etc.
As far as dragging stuff between stacks, the Cocoa drag and drop API will probably be the easiest way to go. There should be a whole programming topic explaining the basics in the main page of the Cocoa docs. Your stack views will have to handle mouse down events to initiate a drag, and also drag destination methods to accept drags. Again, there is probably some code you could have in the common view class, with additional code fleshing out specific behavior in the subclasses.
As far as the actual transferring of cards, you'll want to make your own pasteboard type for transferring data between the views as the drag progresses. You'll have to be able to encode which stack the card(s) are coming from and which cards from the stack are being dragged into some kind of data structure, either cramming into an NSData or using an NSDictionary as a property list.
Hope these ideas are somewhat helpful, and feel free to ask further questions.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
yes, standard solitaire, like on windows  I forgot there are different kinds.
So, basically you're suggesting writing a subclass of NSView, then subclassing THAT into the separate views I need. There should be 2, MAYBE three. one for the 7 stacks across the bottom row, 1 for the 4 stacks where all the cards go (I've been calling those SOWinStacks, in case it wasn't apparent), and one for the rest of the deck, 'cuz you can see the 2 cards under the one you flip, most times. I have card images, so I should be able to put them into an NSImage, and then tell drawRect to draw that, correct? So, if that's how you draw 1 image, what do you do when you need to show PARTS of cards, underneath??
|
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 2001
Status:
Offline
|
|
Originally posted by cheerios:
So, if that's how you draw 1 image, what do you do when you need to show PARTS of cards, underneath??
simply draw the cards ordered back-to-front, with a slight offset.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
??? all in the same view? or layer different views on top of eachother?
|
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
Originally posted by cheerios:
??? all in the same view? or layer different views on top of eachother?
You should do all in the same view. Just make a routine that will draw a card at a given point and then call it repeatedly with different points. You can use the same technique for the deck stack, but probably from left to right instead of top to bottom. Or, if you want to be spiffy and efficient about it, you can use -[NSImage drawAtPoint:fromRect:operation:fraction:] and pass in only a portion of the rectangle into the fromRect: parameter, thus drawing only a portion of the image.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
sigh... I'm back. I'm stuck again, too. not STUCK, persay, but I'm havign a hard time figuring out how to get dragging to behave.
Specifically, putting information onto a Pasteboard.
when I create a pasteboard, can I make up my own type? the declareTypes: owner: function has me confused. Why does it take an NSArray as it's arguement? are there only certain things that youcan paste to a baord? I wanna put an NSMutableArray containing card objects onto it, but I'm not seeing a pasteboard data type that would do that, easily.
if you use one of the pasteboards that are always there, ie NSDragPboard, do you have to declare types?
and to actually put something ON the pasteboard, setData:forType: seems to be the way to go, but what the heck is an NSData object??? I am looking at the documentation, but I'm not understanding A) why they'd use it at all, and B) how to make my array into an NSData, adn get it back as an array at the other side.
I'm running out of time for this project, so any info would be awesome! I've already tried Cocoadevcentral.com, and stepwise.com to no avail, and the code in the examples is blowing my mind.  thx in advance!!
|
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
when I create a pasteboard, can I make up my own type?
Yes.
the declareTypes: owner: function has me confused. Why does it take an NSArray as it's arguement?
You can provide the object being dragged in more than one form. For example, when you drag a URL in OmniWeb, we provide it to the pasteboard as a URL object, a string, and (if it's a file: URL) a filename, as well as in a couple of private types. The application accepting the drag can look at the pasteboard and grab the most appropriate data: Finder will treat it as a file reference if it can, the Dock will create one of those little springy-@ icons, any text editing app will just paste the text of the URL, and another view or window within OmniWeb can treat it as moving or copying a bookmark (or some other app-specific action).
if you use one of the pasteboards that are always there, ie NSDragPboard, do you have to declare types?
Yes.
and to actually put something ON the pasteboard, setData:forType: seems to be the way to go, but what the heck is an NSData object???
An arbitrary bag of bytes. It's used in dragging because a drag or copy operation works across applications, and so a lowest-common-denominator form of data is needed.
how to make my array into an NSData, adn get it back as an array at the other side.
If your array contains only NSStrings and/or NSNumbers, you can easily get it into an NSData and back via NSString and the property list format. See the Cocoa Tasks and Concepts documentation for more on that.
However, since I presume you only care about dragging within your application -- within the same view even, you don't need to worry about putting data on the pasteboard. Keep a pointer to the dragged objects when you start a drag, and refer back to it when you receive the drag. You'll still need to declare types in order for the AppKit machinery to send you drag destination messages, though.
In fact, if your entire solitaire board is one view, you might not even want to deal with NSDragging at all. If you can't drag a card to any location outside of that view, it's a little misleading to allow the dragged card image to travel anywhere on the screen. See /Developer/Examples/AppKit/Sketch for an example of how to do dragging within a view: it's a bit easier in some ways (no messing with NSDragging et al) and a bit harder in others (more drawing work).
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|